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

#define int long long
int dp[6000 + 10][12000 + 20];

int32_t main() {
	int n, MOD;
	cin >> n >> MOD;
	dp[0][3 * n] = 1;
	for (int i = 0; i < 3 * n; i++)
		for (int j = -3 * n; j <= 3 * n; j++) {
			dp[i + 1][j + 1 + 3 * n] = (dp[i + 1][j + 1 + 3 * n] + dp[i][j + 3 * n]) % MOD;
			dp[i + 2][j - 1 + 3 * n] = (dp[i + 2][j - 1 + 3 * n] + dp[i][j + 3 * n] * (i + 1) % MOD) % MOD;
			dp[i + 3][j + 0 + 3 * n] = (dp[i + 3][j + 0 + 3 * n] + dp[i][j + 3 * n] * (i + 1) * (i + 2) % MOD) % MOD;
		}
	int ans = 0;
	for (int i = 0; i <= 3 * n; i++)
		(ans += dp[3 * n][i + 3 * n]) %= MOD;
	cout << ans;
}
