#include <bits/stdc++.h>
#define f(i, j) f[i][j+3000]
#define int long long 
using namespace std;
const int N = 6100;
int n, P;
int f[N][N+3000];
signed main(void){
	cin >> n >> P;
	f(0, 0) = 1;
	for (int i = 0; i < n * 3; ++ i)
		for (int j = -n; j <= 3 * n; ++ j) {
			f(i+1, j+1) = (f(i+1, j+1) + f(i, j)) % P;
			f(i+2, j-1) = (f(i+2, j-1) + f(i, j) * (i + 1) % P) % P;
			f(i+3, j) = (f(i+3, j) + f(i, j) * (i + 1) % P * (i + 2) % P) % P; 
		}
	int res = 0;
	for (int i = 0; i <= n; ++ i) res = (res + f(n*3, i*3)) % P;
	cout << res << endl;
	return 0;
}