#include<bits/stdc++.h>
#define STDIO
#define MOD 1000000007
#define testbit(mask,i) (((mask)>>(i))&1)
#define setbit(mask,i) ((mask)|(1<<i))
#define delbit(mask,i) ((mask)^(1<<i))
#define lowbit(x) ((x)&(-x))
#define INF MOD
using namespace std;
int n;
long long m;
long long finv[10010],p3[10010],p2[10010];
long long inverse(long long x){
	long long ret=1LL;
	int bs=m-2;
	while(bs){
		if(bs&1)
			ret=(ret*x)%m;
		x=(x*x)%m;
		bs>>=1;
	}
	return ret;
}
int main(){
	#ifndef STDIO
		freopen("input.in","r",stdin);
		freopen("output.out","w",stdout);
	#endif
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n>>m;
	finv[0]=1LL;
	for(int i=1;i<=10000;i++){
		finv[i]=(finv[i-1]*inverse(i))%m;
	}
	p3[0]=1LL;
	for(int i=1;i<=10000;i++){
		p3[i]=(p3[i-1]*inverse(3))%m;
	}
	p2[0]=1LL;
	for(int i=1;i<=10000;i++){
		p2[i]=(p2[i-1]*inverse(2))%m;
	}
	long long s=0LL;
	for(int i=0;i<=n;i++){
		for(int j=0;j<=n-i;j++){
			s=(s+(finv[i]*finv[j]%m*finv[3*n-3*i-2*j]%m*p3[i]%m*p2[j]%m))%m;
		}
	}
	for(int i=1;i<=n*3;i++){
		s=(s*i)%m;
	}
	cout<<s<<endl;
	return 0;
}
