#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
ll mod;
const int mn=6e3+10;
ll fact[mn],ifact[mn],pi2[mn],pi6[mn],p2[mn];
ll po(ll a,ll b=mod-2){
    ll ans=1;
    for(;b;b>>=1,a*=a,a%=mod)if(b&1)ans*=a,ans%=mod;
    return ans;
}
int main(){
    int n;
    cin>>n>>mod;
    fact[0]=ifact[0]=pi2[0]=pi6[0]=p2[0]=1;
    for(int i=1;i<=3*n;i++)fact[i]=fact[i-1]*i%mod,ifact[i]=po(fact[i]),p2[i]=p2[i-1]*2%mod;
    pi2[1]=po(2),pi6[1]=po(6);
    for(int i=2;i<=3*n;i++)pi2[i]=pi2[i-1]*pi2[1]%mod,pi6[i]=pi6[i-1]*pi6[1]%mod;
    ll ans=0;
    for(int i=0;i<=n;i++)for(int j=0;i+j<=n;j++){
        ans+=fact[3*n]*pi6[i]%mod*pi2[j]%mod*ifact[i]%mod*ifact[j]%mod*ifact[3*n-3*i-2*j]%mod*p2[i]%mod;
        ans%=mod;
    }
    printf("%lld",ans);
}