#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);
  dp->setmod(M);
  walloc1d(&dp, 2*N+1);
  dp = dp + N;
  walloc1d(&nx1, 2*N+1);
  nx1 = nx1 + N;
  walloc1d(&nx2, 2*N+1);
  nx2 = nx2 + N;
  walloc1d(&nx3, 2*N+1);
  nx3 = nx3 + N;
  for(i=(-N);i<(N+1);i++){
    dp[i] = nx1[i] = nx2[i] = nx3[i] = 0;
  }
  dp[0] = 1;
  for(k=(0);k<(3*N);k++){
    for(i=(-N);i<(N+1);i++){
      nx1[min_L(N, i+1)] += dp[i];
      if(i-1 >= -N){
        nx2[i-1] += dp[i] * (k+1);
      }
      nx3[i] += dp[i] * (k+1) * (k+2);
    }
    {
      auto xr20shxY = (nx1);
      auto WYIGIcGE = (nx2);
      auto t_ynMSdg = (nx3);
      auto KrdatlYV = (dp);
      dp = xr20shxY;
      nx1 = WYIGIcGE;
      nx2 = t_ynMSdg;
      nx3 = KrdatlYV;
    }
    for(i=(-N);i<(N+1);i++){
      nx3[i] = 0;
    }
  }
  {
    int a2conNHc;
    modint hCmBdyQB;
    if(N+1==0){
      hCmBdyQB = 0;
    }
    else{
      hCmBdyQB = dp[0];
      for(a2conNHc=(1);a2conNHc<(N+1);a2conNHc++){
        hCmBdyQB += dp[a2conNHc];
      }
    }
    wt_L(hCmBdyQB);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20200322-1 [beta]

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


Measured memory_bytes: 384