#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; ll M; cin >> N >> M;
    vector<vector<ll>> dp(3*N+1,vector<ll>(N+1,0));
    dp[0][0]=1;
    for (int i=0;i<3*N;++i){
        for (int j=0;j<=N;++j){
            (dp[i+1][j]+=dp[i][j])%=M;
            if (i+2<=3*N&&j+1<=N) (dp[i+2][j+1]+=dp[i][j]*(i+1)%M)%=M;
            if (i+3<=3*N&&j+1<=N) (dp[i+3][j+1]+=dp[i][j]*(i+2)*(i+1)%M)%=M;
        }
    }
    ll ans=0;
    for (int j=0;j<=N;++j) (ans+=dp[3*N][j])%=M;
    cout << ans << '\n';
}