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

typedef long long Int;
typedef pair<Int,Int> PII;
typedef vector<int> VInt;
 
#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define MP make_pair

#define MAX (6 << 10)

Int R[MAX][MAX + MAX];

int main()
{
	int n, m;
	cin >> n >> m;

	CLEAR(R, 0);
	R[0][MAX] = 1;
	int i, j;
	FOR(i, 0, 3*n)
	FOR(j, 0, MAX + MAX)
	if(R[i][j] != 0)
	{
		R[i + 1][j + 1] = (R[i + 1][j + 1] + R[i][j]) % m;
		if(i + 2 <= 3*n) R[i + 2][j - 1] = (R[i + 2][j - 1] + R[i][j]*(i + 1)) % m;
		if(i + 3 <= 3*n) R[i + 3][j] = (R[i + 3][j] + R[i][j]*(i + 1)*(i + 2)) % m;
	}

	Int res = 0;
	FOR(i, MAX, MAX + MAX) res += R[3*n][i];
	res %= m;
	cout << res << endl;
	return 0;
}
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);

#define l_ength size

ll MOD;

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

ll dp[6060][13000];

int main(void){
	int n,i,j;
	ll ans = 0ll;
	std::cin >> n >> MOD;
	n *= 3;
	dp[0][6500] = 1ll;
	for(i=0; i<n; ++i){
		for(j=500; j<=12500; ++j){
			add_mod(dp[i+1][j-1],dp[i][j]);
			add_mod(dp[i+2][j+1],dp[i][j]*(i+1)%MOD);
			add_mod(dp[i+3][j],dp[i][j]*(i+1)*(i+2)%MOD);
		}
	}
	for(j=500; j<=6500; ++j){
		add_mod(ans,dp[n][j]);
	}
	std::cout << ans << std::endl;
	return 0;
}
