#include <cstdio>

using namespace std;

long long dp[6001][2001];

int main() {
    int n, m, i, j;
    long long ans = 0;
    
    scanf("%d %d", &n, &m);
    
    dp[0][0] = 1;
    
    for (i = 1; i <= n * 3; i++) {
        for (j = 0; j <= n; j++) {
            dp[i][j] += dp[i - 1][j];
            if (dp[i][j] >= m) dp[i][j] -= m;
            
            if (i >= 2 && j > 0) {
                dp[i][j] += dp[i - 2][j - 1] * (i - 1) % m;
                if (dp[i][j] >= m) dp[i][j] -= m;
            }
            
            if (i >= 3 && j > 0) {
                dp[i][j] += dp[i - 3][j - 1] * (i - 1) % m * (i - 2) % m;
                if (dp[i][j] >= m) dp[i][j] -= m;
            }
        }
    }
    
    for (i = 0; i <= n; i++) ans += dp[n * 3][i];
    printf("%lld\n", ans % m);
    
    return 0;
}
