#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <numeric>
#include <bitset>
#include <ext/algorithm>
#include <ext/numeric>
#define ffor(_a,_f,_t) for(int _a=(_f),__t=(_t);_a<__t;_a++)
#define all(_v) (_v).begin() , (_v).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define SET(__set, val) memset(__set, val, sizeof(__set))
#define FOR(__i, __n) ffor (__i, 0, __n)
typedef long long LL; using namespace std;

int N, M;
const int OFFSET = 2000;

LL dp[6001][8001];

LL memo(int total, int dif) {
	if (dif < 0)
		return 0LL;
	if (!total)
		return dif >= OFFSET;
		
	LL &ret = dp[total][dif];
	if (ret != -1)
		return ret;
	
	ret = 0LL;
	
	if (total >= 3)
		ret += (total - 1LL) * (total - 2LL) * memo(total - 3, dif);
	if (total >= 2)
		ret += (total - 1LL) * memo(total - 2, dif - 1);
	ret += memo(total - 1, dif + 1);
	
	ret %= M;
	return ret;
}


int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
	
	cin >> N >> M;
	
	SET(dp, 255);
	
	cout << memo(3 * N, OFFSET) << endl;
	
	return 0;
}
#include<set>    
#include<map>    
#include<stack>    
#include<ctime>    
#include<cstdio>    
#include<queue>    
#include<cmath>    
#include<vector>    
#include<cstring>   
#include<climits>    
#include<iostream>   
#include<algorithm>
using namespace std;
#define LL long long
int read(){
    int f=1,x=0;char c=getchar();
    while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();}
    while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();}
    return f*x;
}
#define MAXN 12000
#define INF 0x3f3f3f3f
int Mod;
int Pow(int x,int y){
	int ret=1;
	while(y){
		if(y&1) ret=1ll*ret*x%Mod;
		x=1ll*x*x%Mod,y>>=1;
	}
	return ret;
}
int f[MAXN+5][MAXN+5],inv[MAXN+5];
int main(){
	int n=read(),O=3*n;
	Mod=read();
	for(int i=1;i<=3*n;i++)
		inv[i]=Pow(i,Mod-2);
	f[0][O]=1;
	for(int i=1;i<=3*n;i++)
		for(int j=0;j<=6*n;j++){
			f[i][j]+=f[i-1][j-1];
			if(f[i][j]>=Mod) f[i][j]-=Mod;
			if(i>=2)
				f[i][j]+=f[i-2][j+1];
			if(f[i][j]>=Mod) f[i][j]-=Mod;
			if(i>=3)
				f[i][j]+=f[i-3][j];
			if(f[i][j]>=Mod) f[i][j]-=Mod;
			f[i][j]=1ll*f[i][j]*inv[i]%Mod;
		}
	int ans=0,n3=1;
	for(int i=1;i<=3*n;i++)
		n3=1ll*n3*i%Mod;
	for(int i=O;i<=6*n;i++)
		ans=(ans+1ll*n3*f[3*n][i]%Mod)%Mod;
	printf("%d\n",ans);
	return 0;
}
