#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
clock_t __my_start_clock = clock();
#else
#define eprintf(...)
#endif

#define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)
#define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)

#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")\n"
#define dbv(a) cerr << #a << " = "; for (auto xxxx: a) cerr << xxxx << " "; cerr << endl


using namespace std;

#ifdef LOCAL
static struct __timestamper {
    string what;
    __timestamper(const char* what) : what(what){};
    __timestamper(const string& what) : what(what){};
    ~__timestamper(){
        TIMESTAMPf("%s", what.data());
    }
} __TIMESTAMPER("end");
#else
struct __timestamper {};
#endif

typedef long long ll;
typedef long double ld;

int MOD;
void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
};
int mul(int x, int y) {
    return 1LL * x * y % MOD;
};

int slow(int n) {
    /*vector<vector<vector<vector<int>>>> dp(n + 1, vector<vector<vector<int>>>(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1))));
    dp[n][0][0][0] = 1;
    for (int c3 = n; c3 >= 0; c3--) {
        for (int c2 = n; c2 >= 0; c2--) {
            for (int c1 = n; c1 >= 0; c1--) {
                for (int c0 = n; c0 >= 0; c0--) {
                    int cur = dp[c3][c2][c1][c0];
                    if (cur == 0) continue;
                    int placed = 3 * n - 3 * c3 - 2 * c2 - 1 * c1;

                    if (c1) {
                        add(dp[c3][c2][c1 - 1][c0 + 1], cur);
                    }
                    if (c2) {
                        add(dp[c3][c2 - 1][c1 + 1][c0], cur);
                        add(dp[c3][c2 - 1][c1][c0 + 1], mul(cur, placed + 1));
                    }
                    if (c3) {
                        add(dp[c3 - 1][c2 + 1][c1][c0], cur);
                        add(dp[c3 - 1][c2][c1 + 1][c0], mul(cur, placed + 1));
                        add(dp[c3 - 1][c2][c1][c0 + 1], mul(cur, (placed + 1) * (placed + 2)));
                    }
                }
            }
        }
    }
    return dp[0][0][0][n];*/
//    vector<vector<vector<int>>> dp(3 * n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1)));
//    dp[0][0][0] = 1;
    vector<vector<int>> dp(3 * n + 1, vector<int>(n + 1));
    dp[0][0] = 1;
    for (int i = 0; i < 3 * n; i++) {
        for (int j = 0; j <= n; j++) {
            if (dp[i][j] == 0) continue;
            add(dp[i + 1][j], dp[i][j]);
            if (i + 2 <= 3 * n && j + 1 <= n) {
                add(dp[i + 2][j + 1], mul(dp[i][j], i + 1));
            }
            if (i + 3 <= 3 * n && j + 1 <= n) {
                add(dp[i + 3][j + 1], mul(dp[i][j], (i + 1) * (i + 2)));
            }
        }
    }
    int ans = 0;
    for (int j = 0; j <= n; j++) add(ans, dp[3 * n][j]);
    return ans;
}

int main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif

    int n;
    while (cin >> n >> MOD) {
        cout << slow(n) << endl;
    }

    return 0;
}


Measured memory_bytes: 47360