#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

vector<int> fact;
vector<int> ifact;
vector<int> inv;
vector<int> pow2;
int MOD;
int add(int a, int b)
{
	a+=b;
	while(a>=MOD) a-=MOD;
	return a;
}
void radd(int &a, int b)
{
	a=add(a,b); 
}
int mult(int a, int b)
{
	return (a*1LL*b)%MOD;
}
void rmult(int &a, int b)
{
	a=mult(a,b);
}
int modpow(int a, int b)
{
	int r=1;
	while(b)
	{
		if(b&1) r=mult(r,a);
		a=mult(a,a);
		b>>=1;
	}
	return r;
}
int choose(int a, int b)
{
	if(a<b) return 0;
	if(b==0) return 1;
	if(a==b) return 1;
	return mult(fact[a],mult(ifact[b],ifact[a-b]));
}
int inverse(int a)
{
	return modpow(a,MOD-2);
}
void init(int _n)
{
	fact.clear(); ifact.clear(); inv.clear(); pow2.clear();
	fact.resize(_n+1);
	ifact.resize(_n+1);
	inv.resize(_n+1);
	pow2.resize(_n+1);
	pow2[0]=1;
	ifact[0]=1;
	fact[0]=1;
	for(int i=1;i<=_n;i++)
	{
		pow2[i]=add(pow2[i-1],pow2[i-1]);
		fact[i]=mult(fact[i-1],i);
		//ifact[i]=mult(ifact[i-1],inv[i]);
	}
	ifact[_n] = inverse(fact[_n]);
	for(int i=_n-1;i>=1;i--)
	{
		ifact[i] = mult(ifact[i + 1], i + 1);
	}
	for(int i=1;i<=_n;i++)
	{
		inv[i] = mult(fact[i-1],ifact[i]);
	}
}

//const int N = 230;
//int dp[N+11][N+11][N+11];
int dp3[6222][6222]; 
int dp2[6222][6222]; 
int hook[6222];

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n>>MOD;
	init(288000);
	dp2[0][0]=1;
	for(int i=0;i<3*n;i++)
	{
		for(int j=0;j<=3*n;j++)
		{
			if(dp2[i][j]==0) continue;
			int v=dp2[i][j];
			radd(dp2[i+1][j+1],v);
			if(j>=1) radd(dp2[i+1][j-1],mult(j,v));
		}
	}
	/*
	dp3[0][0]=1;
	for(int i=0;i<3*n;i++)
	{
		for(int j=0;j<=3*n;j++)
		{
			if(dp3[i][j]==0) continue;
			int v=dp3[i][j];
			radd(dp3[i+1][j+1],v);
			if(j>=2) radd(dp3[i+1][j-2],v);
		}
	}
	*/
	/*
	hook[0]=1;
	for(int i=1;i<=n;i++)
	{
		//use hook length formula!? wrong =_=
		hook[i]=fact[3*i];
		for(int j=1;j<=i;j++)
		{
			hook[i]=mult(hook[i],inv[j]);
			hook[i]=mult(hook[i],inv[j+1]);
			hook[i]=mult(hook[i],inv[j+2]);
		}
		hook[i]=mult(hook[i],pow2[i]);
		cerr<<i<<' '<<hook[i]<<'\n';
	}
	*/
	hook[0]=1;
	for(int i=1;i<=n;i++)
	{
		hook[i]=mult(fact[3*i],ifact[i]);
		for(int j=1;j<=i;j++)
		{
			hook[i]=mult(hook[i],inv[3]);
		}
	}
	int ans=0;
	for(int a=0;a<=n;a++)
	{
		for(int b=0;a+b<=n;b++)
		{
			int c = 3*n-3*a-2*b;
			int coeff = mult(fact[n*3],mult(ifact[3*a], mult(ifact[2*b], ifact[c])));
			//cerr<<a<<' '<<b<<' '<<c<<' '<<coeff<<'\n';
			//cerr<<hook[a]<<' '<<dp2[2*b][0]<<'\n';
			coeff = mult(coeff, hook[a]);
			//coeff = mult(coeff, dp3[3*a][0]); //needs a fix
			coeff = mult(coeff, dp2[2*b][0]);
			radd(ans,coeff);
		}
	}
	/*
	dp[0][0][0]=1;
	for(int i=1;i<=3*n;i++) dp[0][i][0]=1;
	*/
	//O(n^3)
	
	//optimize
	//later use PIE
	//dp[i][j][k] = # of sequences of length i, max element <= j, k bumps
	/*
	for(int i=1;i<=3*n;i++)
	{
		for(int j=0;j<=3*n;j++)
		{
			for(int k=0;k<=n;k++)
			{
				radd(dp[i][j][k],dp[i-1][j-1][k]); //chain 1
				if(i>=2&&k>0) radd(dp[i][j][k],mult(dp[i-2][j-1][k-1],j-(i-1)));
				if(i>=3&&k>0) radd(dp[i][j][k],mult(dp[i-3][j-1][k-1],mult(j-(i-1),j-(i-2))));
				if(j>0) radd(dp[i][j][k],dp[i][j-1][k]);
			}
		}
	}
	*/
	/*
	for(int i=0;i<3*n;i++)
	{
		for(int j=0;j<=3*n;j++)
		{
			for(int k=0;k<=n;k++)
			{
				int v=dp[i][j][k];
				if(v==0) continue;
				//add a chain of length 1
				for(int l=j+1;l<=3*n;l++)
				{
					radd(dp[i+1][l][k],v);
				}
				//add a chain of length 2
				for(int l=j+1;l<=3*n;l++)
				{
					int coeff = l-(i+1); //numbers <= l, i+1 used
					radd(dp[i+2][l][k+1],mult(v,coeff));
				}
				//add a chain of length 3
				for(int l=j+1;l<=3*n;l++)
				{
					int coeff = 0;
					if(l>=i+2) coeff = mult(l-(i+1),l-(i+2)); //numbers <= l, i+1 used
					radd(dp[i+3][l][k+1],mult(v,coeff));
				}
			}
		}
	}
	*/
	cout<<ans<<'\n';
}


Measured memory_bytes: 150528