#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI=acos(-1.0);
#define t1(x)             cerr<<#x<<"="<<x<<endl
#define t2(x, y)          cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define t3(x, y, z)       cerr<<#x<<"=" <<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define t4(a,b,c,d)       cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<endl
#define t5(a,b,c,d,e)     cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<endl
#define t6(a,b,c,d,e,f)   cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<" "<<#f<<"="<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define __ freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define fastio() ios::sync_with_stdio(0);cin.tie(0)
#define MEMS(x,t) memset(x,t,sizeof(x));
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*-------------------------------------------------------------------------------------------------------------------------------------*/
// #define MOD 1000000007
#define endl "\n" 
#define int long long
#define inf 1e18
#define ld long double

/*-------------------------------------------------------------------------------------------------------------------------------------*/

int dp[6005][12005];
int n,MOD;
int rec(int ind,int balance)
{
	if(ind==0)
	{
		return (balance>=0 && balance%3==0);
	}
	int &ans=dp[ind][balance+3*n];
	if(ans>=0)return ans;
	ans=rec(ind-1,balance+1);
	if(ind>1)
		ans+=(ind-1)*rec(ind-2,balance-1);
	if(ind>2)
		ans+=(ind-1)*(ind-2)*rec(ind-3,balance);
	ans%=MOD;
	return ans;
}
int32_t main()
{
	fastio();
	cin>>n>>MOD;
	MEMS(dp,-1);
	cout<<rec(3*n,0)<<endl;
}

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define re register
#define LL long long
// #define MOD 1000000007
#define MAXN 6005
using namespace std;
const int ST=6002;
LL n,ans,MOD,dp[MAXN][MAXN<<1];
LL rd(){
	LL x=0,tp=1;char c;c=getchar();
	while(c<'0' || c>'9'){if(c=='-')tp=-1;c=getchar();}
	while(c>='0' && c<='9'){x=(x*10+c-'0');c=getchar();}
	return x*tp;
}
int add(int a,int b){return (a+b>=MOD)?(a+b-MOD):a+b;}
int main(){
	// freopen("testdata.in","r",stdin);
	// freopen("shit.out","w",stdout);
	n=rd()*3,MOD=rd();
	dp[0][ST]=1;
	for(int i=0;i<n;i++){
		for(int j=-i;j<=i;j++){
			dp[i+1][ST+j+1]=add(dp[i+1][ST+j+1],dp[i][ST+j]);
			dp[i+2][ST+j-1]=add(dp[i+2][ST+j-1],dp[i][ST+j]*(i+1)%MOD);
			dp[i+3][ST+j]=add(dp[i+3][ST+j],dp[i][ST+j]*(i+1)%MOD*(i+2)%MOD);
		}
	}
	for(int i=0;i<=n;i++)ans=add(ans,dp[n][ST+i]);
	printf("%lld\n",ans);
	// printf("%.2lf\n",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}