#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 6100;

int N, T;
ll M;
ll dp[MAXN][MAXN];

int main()
{
    ios_base::sync_with_stdio(0);

    cin >> N >> M;
    T = 3 * N;

    dp[0][0] = 1;

    for (int i = 0; i <= T; i++)
    {
        for (int j = 0; j <= T; j++)
        {
            dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % M;
            dp[i+2][j+1] = (dp[i+2][j+1] + dp[i][j] * (i + 1)) % M;
            dp[i+3][j+1] = (dp[i+3][j+1] + dp[i][j] * (i + 1) * (i + 2)) % M;
        }
    }

    ll res = 0;
    for (int i = 0; i <= N; i++)
        res = (res + dp[T][i]) % M;
    cout << res << "\n";
}