#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
int n;
long long D[6020][2010], res, Mod;
int main() {
	int i, j, k, s;
	scanf("%d%lld", &n, &Mod);
	D[0][0] = 1;
	for (i = 0; i <= 3 * n; i++) {
		for (j = 0; j <= n; j++) {
			D[i][j] %= Mod;
			D[i + 1][j] += D[i][j];
			D[i + 2][j + 1] += D[i][j] * (i+1);
			D[i + 3][j + 1] += D[i][j] * (i+1) * (i + 2);
		}
	}
	for (i = 0; i <= n; i++)res = (res + D[3*n][i]) % Mod;
	printf("%lld\n", res);
}