#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//ModInt::set(M)
struct ModInt {
    using u64 = uint_fast64_t;

    u64 a;

    ModInt(long long x = 0) : a(x >= 0 ? x % Modulus() : (Modulus() - (-x) % Modulus()) % Modulus()) {}

    static u64 &Modulus() {static u64 Modulus = 0; return Modulus;}
    static void set (u64 M) {Modulus() = M;}

    ModInt operator+(const ModInt rhs) const noexcept {return ModInt(*this) += rhs;}
    ModInt operator-(const ModInt rhs) const noexcept {return ModInt(*this) -= rhs;}
    ModInt operator*(const ModInt rhs) const noexcept {return ModInt(*this) *= rhs;}
    ModInt operator/(const ModInt rhs) const noexcept {return ModInt(*this) /= rhs;}
    ModInt operator^(const long long rhs) const noexcept {return ModInt(*this) ^= rhs;}
    bool operator==(const ModInt &rhs) const noexcept {return a == rhs.a;}
    bool operator!=(const ModInt &rhs) const noexcept {return a != rhs.a;}
    ModInt &operator+=(const ModInt rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus()) {
            a -= Modulus();
        }
        return *this;
    }
    ModInt &operator-=(const ModInt rhs) noexcept {
        if (a < rhs.a) {
            a += Modulus();
        }
        a -= rhs.a;
        return *this;
    }
    ModInt &operator*=(ModInt rhs) noexcept {
        a = a * rhs.a % Modulus();
        return *this;
    }
    ModInt &operator/=(ModInt rhs) noexcept {
        u64 exp = Modulus() - 2;
        while (exp) {
            if (exp&1) *this *= rhs;
            exp >>= 1;
            rhs *= rhs;
        }
        return *this;
    }
    ModInt &operator^=(long long exp) noexcept {
        ModInt rhs = a;
        a = 1;
        while (exp) {
            if (exp&1) *this *= rhs;
            exp >>= 1;
            rhs *= rhs;
        }
        return *this;
    }

    friend ostream &operator<<(ostream& os, const ModInt& rhs) noexcept {return os << rhs.a;}
    friend istream &operator>>(istream& is, ModInt& rhs) noexcept {long long a; is >> a; rhs = a; return is;}
};

using mint = ModInt;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,M; cin >> N >> M;
    ModInt::set(M);

    vector<vector<mint>> dp(3*N+1, vector<mint>(6*N+1));
    dp[3*N][3*N] = 1;
    for (int i = 3*N; i > 0; i--) {
        rep(j,6*N+1) {
            if (dp[i][j].a==0) continue;
            if (i >= 3) dp[i-3][j] += dp[i][j]*(i-1)*(i-2);
            if (j > 0 and i >= 2) {
                dp[i-2][j-1] += dp[i][j]*(i-1);
            }
            if (j < 6*N) {
                dp[i-1][j+1] += dp[i][j];
            }
        }
    }

    mint ans = 0;
    for (int i = 3*N; i <= 6*N; i++) ans += dp[0][i];

    cout << ans << ln;
}


Measured memory_bytes: 566048