#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define MD (1000000007U)
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
struct modint{
  static unsigned md;
  unsigned val;
  modint(){
    val=0;
  }
  modint(int a){
    val = ord(a);
  }
  modint(unsigned a){
    val = ord(a);
  }
  modint(long long a){
    val = ord(a);
  }
  modint(unsigned long long a){
    val = ord(a);
  }
  void setmod(unsigned m){
    md = m;
  }
  unsigned ord(unsigned a){
    return a%md;
  }
  unsigned ord(int a){
    a %= (int)md;
    if(a < 0){
      a += md;
    }
    return a;
  }
  unsigned ord(unsigned long long a){
    return a%md;
  }
  unsigned ord(long long a){
    a %= (int)md;
    if(a < 0){
      a += md;
    }
    return a;
  }
  unsigned get(){
    return val;
  }
  modint &operator+=(modint a){
    val += a.val;
    if(val >= md){
      val -= md;
    }
    return *this;
  }
  modint &operator-=(modint a){
    if(val < a.val){
      val = val + md - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  modint &operator*=(modint a){
    val = ((unsigned long long)val*a.val)%md;
    return *this;
  }
  modint &operator/=(modint a){
    return *this *= a.inverse();
  }
  modint operator+(modint a){
    return modint(*this)+=a;
  }
  modint operator-(modint a){
    return modint(*this)-=a;
  }
  modint operator*(modint a){
    return modint(*this)*=a;
  }
  modint operator/(modint a){
    return modint(*this)/=a;
  }
  modint operator+(int a){
    return modint(*this)+=modint(a);
  }
  modint operator-(int a){
    return modint(*this)-=modint(a);
  }
  modint operator*(int a){
    return modint(*this)*=modint(a);
  }
  modint operator/(int a){
    return modint(*this)/=modint(a);
  }
  modint operator+(long long a){
    return modint(*this)+=modint(a);
  }
  modint operator-(long long a){
    return modint(*this)-=modint(a);
  }
  modint operator*(long long a){
    return modint(*this)*=modint(a);
  }
  modint operator/(long long a){
    return modint(*this)/=modint(a);
  }
  modint operator-(void){
    modint res;
    if(val){
      res.val=md-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  operator bool(void){
    return val!=0;
  }
  operator int(void){
    return get();
  }
  operator long long(void){
    return get();
  }
  modint inverse(){
    int a = val;
    int b = md;
    int u = 1;
    int v = 0;
    int t;
    modint res;
    while(b){
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if(u < 0){
      u += md;
    }
    res.val = u;
    return res;
  }
  modint pw(unsigned long long b){
    modint a(*this);
    modint res;
    res.val = 1;
    while(b){
      if(b&1){
        res *= a;
      }
      b >>= 1;
      a *= a;
    }
    return res;
  }
  bool operator==(int a){
    return ord(a)==val;
  }
  bool operator!=(int a){
    return ord(a)!=val;
  }
}
;
unsigned modint::md;
modint operator+(int a, modint b){
  return modint(a)+=b;
}
modint operator-(int a, modint b){
  return modint(a)-=b;
}
modint operator*(int a, modint b){
  return modint(a)*=b;
}
modint operator/(int a, modint b){
  return modint(a)/=b;
}
modint operator+(long long a, modint b){
  return modint(a)+=b;
}
modint operator-(long long a, modint b){
  return modint(a)-=b;
}
modint operator*(long long a, modint b){
  return modint(a)*=b;
}
modint operator/(long long a, modint b){
  return modint(a)/=b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(modint x){
  int i;
  i = (int)x;
  wt_L(i);
}
int N;
int M;
int mx;
modint *dp;
modint *nx1;
modint *nx2;
modint *nx3;
int main(){
  int i, k;
  wmem = memarr;
  {
    modint x;
    x.setmod(MD);
  }
  rd(N);
  rd(M);
  mx = N;
  N *= 3;
  dp->setmod(M);
  walloc1d(&dp, 2*mx+1);
  dp = dp + mx;
  walloc1d(&nx1, 2*mx+1);
  nx1 = nx1 + mx;
  walloc1d(&nx2, 2*mx+1);
  nx2 = nx2 + mx;
  walloc1d(&nx3, 2*mx+1);
  nx3 = nx3 + mx;
  for(i=(-mx);i<(mx+1);i++){
    dp[i] = nx1[i] = nx2[i] = nx3[i] = 0;
  }
  dp[0] = 1;
  for(k=(0);k<(N);k++){
    for(i=(-mx);i<(mx+1);i++){
      nx1[min_L(i+1, mx)] += dp[i];
      if(i-1 >= -mx){
        nx2[i-1] += dp[i] * (k+1);
      }
      nx3[i] += dp[i] * (k+1) * (k+2);
    }
    for(i=(-mx);i<(mx+1);i++){
      dp[i] = nx1[i];
    }
    for(i=(-mx);i<(mx+1);i++){
      nx1[i] = nx2[i];
    }
    for(i=(-mx);i<(mx+1);i++){
      nx2[i] = nx3[i];
    }
    for(i=(-mx);i<(mx+1);i++){
      nx3[i] = 0;
    }
  }
  {
    int V9aVTaxx;
    modint APIVbQlN;
    if(mx+1==0){
      APIVbQlN = 0;
    }
    else{
      APIVbQlN = dp[0];
      for(V9aVTaxx=(1);V9aVTaxx<(mx+1);V9aVTaxx++){
        APIVbQlN += dp[V9aVTaxx];
      }
    }
    wt_L(APIVbQlN);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20200322-1 [beta]

// --- original code ---
// int N, M, mx;
// modint *dp, *nx1, *nx2, *nx3;
// {
//   rd(N,M);
//   mx = N;
//   N *= 3;
//   dp->setmod(M);
// 
//   walloc1d(&dp, 2mx+1); dp = dp + mx;
//   walloc1d(&nx1, 2mx+1); nx1 = nx1 + mx;
//   walloc1d(&nx2, 2mx+1); nx2 = nx2 + mx;
//   walloc1d(&nx3, 2mx+1); nx3 = nx3 + mx;
// 
//   rep(i,-mx,mx+1) dp[i] = nx1[i] = nx2[i] = nx3[i] = 0;
//   dp[0] = 1;
// 
//   rep(k,N){
//     rep(i,-mx,mx+1){
//       nx1[min(i+1,mx)] += dp[i];
//       if(i-1 >= -mx) nx2[i-1] += dp[i] * (k+1);
//       nx3[i] += dp[i] * (k+1) * (k+2);
//     }
//     rep(i,-mx,mx+1) dp[i] = nx1[i];
//     rep(i,-mx,mx+1) nx1[i] = nx2[i];
//     rep(i,-mx,mx+1) nx2[i] = nx3[i];
//     rep(i,-mx,mx+1) nx3[i] = 0;
//   }
//   wt(sum(dp(mx+1)));
// }


Measured memory_bytes: 256