R,O=range,print N,P=map(int,input().split()) L=N+1 def C(f,g): return [sum(f[j]*g[i-j]for j in R(0,i+1))%P for i in R(L)] if N<4:O([1,3,18][N-1]%P),exit() F=[1] G=[1] I=[1] for i in R(1,L): F+=[F[-1]*i%P] I+=[[1,P-P//i*I[P%i]%P][i>1]] G+=[G[-1]*I[i]%P] f=[0]+[pow(d,d-2,P)*G[d]%P for d in R(1,L)] g=[[1]+[0]*N,[f[d]*d%P for d in R(L)]] K=32 for k in R(K-2):g+=[C(g[-1],g[1])] h=[g[0][:],C(g[-1],g[1])] for l in R(N//K-1):h+=[C(h[-1],h[1])] a=k=l=0 M=L e=[0]*L for d in R(L): for n in R(k,M):e[n]+=f[d]*g[k][n] k+=1 if k==K or d==N:k,l,M,a,e=0,l+1,M-K,a+C(e,h[l])[N],[0]*L O(a*F[N]%P)