Problem:
Score : 1200 points

Problem Statement

Given is a positive integer N.
Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below.
This number can be enormous, so print it modulo a prime number M.

Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once.

Let P be an empty sequence, and do the following operation 3N times.

Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x.

Remove x from the sequence, and add x at the end of P.

Constraints

1 \leq N \leq 2000

10^8 \leq M \leq 10^9+7

M is a prime number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of permutations modulo M.

Sample Input 1

1 998244353

Sample Output 1

6

All permutations of length 3 count.

Sample Input 2

2 998244353

Sample Output 2

261

Sample Input 3

314 1000000007

Sample Output 3

182908545

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;++A)
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD M
ll N, M = 1;

struct llm{
    ll value=0;

    //llm(const unsigned ll &val) { (this->value = val) %= MOD; }
    //llm(const unsigned int &val) { (this->value = val) %= MOD; }
    llm(const ll& val=0){(this->value = MOD + val % MOD) %= MOD;}
    //llm(const int& val){(this->value = MOD + val % MOD) %= MOD;}
    llm(const llm& a) { llm(a.value); }
    llm inv(){ll a = 1;ll b = this->value;for(ll m=MOD-2;m>0;m/=2){if(m&1){a=a*b%MOD;}b=b*b%MOD;}return llm(a);}
    llm &operator+=(const llm &a) {(this->value += a.value) %= MOD;return *this;}
    llm &operator-=(const llm &a) {(this->value += MOD - a.value) %= MOD;return *this;}
    llm &operator*=(const llm &a) {(this->value *= a.value) %= MOD; return *this; }
    explicit operator ll() const{return (ll)value;}
    explicit operator int() const{return (int)value;}
};
llm operator+(const llm &a, const llm &b) { return llm(a.value + b.value); }
llm operator-(const llm &a, const llm &b) { return llm(a.value - b.value); }
llm operator*(const llm &a, const llm &b) { return llm(a.value * b.value); }
#define pllm pair<llm,llm>
llm Fa[101010],iFa[101010];
void init(){
    ll i = 0;
    Fa[0] = 1;
    fornum(i,0,100000){
        Fa[i + 1] = Fa[i]*(i + 1);
    }
    iFa[100000] = Fa[100000].inv();
    for (i = 100000; i > 0;--i){
        iFa[i - 1] = iFa[i] * i;
    }
}
llm inv(ll a){
    if(a<=0)
        return 1;
    return iFa[a] * Fa[a - 1];
}


ll i, j, k;

int main(){
    scanf("%lld%lld", &N,&M);
    vector<vector<llm>> dp(N * 3 + 50, vector<llm>(7000));
    init();
    dp[0][3000] = Fa[N * 3];

    fornum(i, 0, N * 3)
    {
        fornum(j, 3000-i/2, 6000)
        {
            dp[i + 1][j + 1] += dp[i][j] * inv(i + 1);
            if(j>0)
                dp[i + 2][j - 1] += dp[i][j] * inv(i + 2);
            dp[i + 3][j] += dp[i][j] * inv(i + 3);
        }
        dp[i + 1][j] += dp[i][j] * inv(i + 1);
        dp[i + 2][j] += dp[i][j] * inv(i + 2);
        dp[i + 3][j] += dp[i][j] * inv(i + 3);
    }
    llm ans = 0;
    fornum(i,3000,7000){
        ans += dp[N * 3][i];
    }
    printf("%lld\n", (ll)ans);

    return 0;
}