#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mod;
ll dp[6666][2222];
int main()
{
	int num;
	scanf("%d%lld", &num, &mod);
	dp[0][0] = 1;
	for (int i = 0; i < num * 3; i++)
	{
		int t = num * 3 - i;
		for (int j = 0; j <= num; j++)
		{
			dp[i + 3][j + 1] = (dp[i + 3][j + 1] + dp[i][j] * (t - 1)*(t - 2)) % mod;
			dp[i + 2][j + 1] = (dp[i + 2][j + 1] + dp[i][j] * (t - 1)) % mod;
			dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod;
		}
	}
	ll ans = 0;
	for (int i = 0; i <= num; i++)ans += dp[num * 3][i];
	printf("%lld\n", ans%mod);
}