#include"bits/stdc++.h" #include"atcoder/all" usingnamespace std; #define fi first #define se second #define vi vector<int> #define pb push_back #define eb emplace_back #define pii pair<int,int> #define mp make_pair #define rep( i , a , b ) for( int i = (a) , i##end = b ; i <= i##end ; ++ i ) #define per( i , a , b ) for( int i = (a) , i##end = b ; i >= i##end ; -- i ) #define mem( a ) memset( a , 0 , sizeof (a) ) #define all( x ) x.begin() , x.end() //#define int long long // #pragma GCC optimize(2) typedeflonglong ll; constint MAXN = 6e3 + 10; int P; int n; int f[MAXN][MAXN * 2] , J[MAXN] , iJ[MAXN] , iv[MAXN];
intPow( int x , int a ){ int ret = 1; while( a ) { if( a & 1 ) ret = ret * 1ll * x % P; x = x * 1ll * x % P , a >>= 1; } return ret; }
voidInc( int& a , int b ){ a = ( a + b < P ? a + b : a + b - P ); }
voidsolve(){ cin >> n >> P; J[0] = 1; rep( i , 1 , n * 3 ) J[i] = J[i - 1] * 1ll * i % P , iv[i] = Pow( i , P - 2 ); iJ[n * 3] = Pow( J[n * 3] , P - 2 ); per( i , n * 3 , 1 ) iJ[i - 1] = iJ[i] * 1ll * i % P; constint B = 3 * n + 1; f[0][B] = 1; rep( i , 0 , n * 3 - 1 ) rep( j , 0 , n * 4 + 2 ) if( f[i][j] ) { Inc( f[i + 2][j + 1] , f[i][j] * 1ll * iv[i + 2] % P ); Inc( f[i + 3][j] , f[i][j] * 1ll * iv[i + 3] % P ); if( j ) Inc( f[i + 1][j - 1] , f[i][j] * 1ll * iv[i + 1] % P ); } int ans = 0; for( int t = 0 ; t <= n * 3 ; t += 3 ) Inc( ans , f[n * 3][B - t] ); cout << ans * 1ll * J[n * 3] % P << endl; }