//khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize ("Ofast")
#pragma GCC optimize("unroll-loops")


using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll INF = 1000000010;
const ll LOG = 25;
const int N = 2000 + 10;
int MOD;

int mul(int a, int b){
	return (a * 1ll * b) % MOD;
}

int add(int a, int b){
	a += b;
	a %= MOD;
	return a;
}

int dp[3 * N][3 * N], n;

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> MOD;
	dp[0][0] = 1;
	for (int i = 1; i <= 3 * n; i++){
		for (int j = 0; j <= i / 2; j++){
			dp[i][j] = add(dp[i][j], dp[i - 1][j]);
			if (j != 0){
				if (i > 1) dp[i][j] = add(dp[i][j], mul(i - 1, dp[i - 2][j - 1]));
				if (i > 2) dp[i][j] = add(dp[i][j], mul(i - 2, mul(i - 1, dp[i - 3][j - 1])));
			}
		}
	}	
	int ans = 0;
	for (int i = 0; i <= n; i++){
		ans = add(ans, dp[3 * n][i]);
	}
	cout << ans;











	return 0;
}

#include"bits/stdc++.h"
#include<unordered_set>
#include<unordered_map>
#include<random>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;//998'244'353;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
int dx[4]={ 1,0,-1,0 };
int dy[4]={ 0,1,0,-1 };

template<typename T>
T power(T x, long long n, const T &m){
    if(n == 0) return 1;
    if(n == 1) return x;
    T tmp = power(x, n/2, m);
    if(n%2 == 0) return tmp * tmp % m;
    else return tmp * tmp % m * x % m;
}

template<typename T>
T inverse(T x, const T &m){
    return power(x, m-2, m);
}

struct Combination{
    int N;
    long long M;
    vector<long long> f, i;
    void init(int n, long long m){
        N = n; M = m;
        f.resize(N+1); f[0] = 1;
        i.resize(N+1); i[0] = 1;
        for(long long j=1; j<=N; j++){
            f[j] = f[j-1]*j%M;
            i[j] = inverse(f[j], M);
        }
    }
    long long fact(int n){
        return f[n];
    }
    long long C(int n, int r){
        return f[n] * i[r] % M * i[n-r] % M;
    }
};

int N;
ll M;

Combination comb;

ll solve(int N){
    ll ans = 0;
    ll tmp = 1;
    rep(i, N/3+1){
        ans += tmp * comb.i[i];
        ans %= M;
        if(i == N/3) continue;
        tmp *= comb.C(N-2*i, 2);
        tmp %= M;
    }
    return ans;
}

signed main(){
    cin >> N >> M;
    comb.init(3*N, M);
    ll ans = 0;
    ll tmp = 1;
    rep(i, N+1){
        ans += solve(3*(N-i)) * tmp % M * comb.i[i];
        ans %= M;
        if(i == N) continue;
        tmp *= comb.C(3*(N-i), 3) * 2;
        tmp %= M;
    }
    cout << ans << endl;
}