#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll dp[51][2611], fdiff[51], nCr[51][51], fac[51];

int main()
{
	ll n, mod;
	cin>>n>>mod;
	dp[1][0]=1;
	
	for(int i=2;i<=n;++i)
	{
		for(int j=1;j<=i;++j)
		{
			for(int inv=i-j;inv<=i*i;++inv)
			{
				dp[i][inv]+=dp[i-1][inv-i+j];
				dp[i][inv]%=mod;
			}
		}
	}
	
	for(int i=3;i<=n;++i)
	{
		for(int diff=1;diff<i;++diff)
		{
			int finv=i*i-diff-1, sinv=i*i;
			ll S=0;
			
			while(finv+1)
			{
				S+=dp[i-1][sinv];
				S%=mod;
				
				ll w=(i-diff);
				w=(w*S)%mod;
				w=(w*dp[i-1][finv])%mod;
				fdiff[i]+=w;
				fdiff[i]%=mod;
				--finv;
				--sinv;
				
			}
		}
	}
	
	nCr[1][0]=1;
	nCr[1][1]=1;
	for(int i=2;i<=n;++i)
	{
		nCr[i][0]=1;
		for(int j=1;j<=i;++j) nCr[i][j]=(nCr[i-1][j]+nCr[i-1][j-1])%mod;
	}
	
	fac[0]=1;
	for(int i=1;i<=n;++i) fac[i]=(fac[i-1]*i)%mod;
	
	ll ans=fdiff[n];
	for(int F=1;F<n-1;++F)
	{
		ll w=(nCr[n][F]*fac[F])%mod;
		ans+=(fdiff[n-F]*w)%mod;
		ans%=mod;
	}
	
	cout<<ans;
	
	
}