#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define ll long long
const int N=514114;
ll as,s,n,md,f[3][N];
ll C(int i,int ty){return i<0?0:f[ty][i];}
int main(){
	scanf("%lld%lld",&n,&md);f[0][0]=1;
	rep(i,1,n){s=0;as=(as*i)%md; 
		rep(j,0,i*(i-1)/2)f[1][j]=(C(j-1,1)+f[0][j])%md,f[2][j]=((C(j-1,2)+f[1][j]))%md;
		rep(j,0,i*(i-1)/2)s=(s+C(j-2,1)*i-C(j-2,2)+C(j-i-2,2)+md)%md,as=(as+s*f[0][j])%md;	
		rep(j,0,i*(i-1)/2)f[0][j]=(C(j,1)-C(j-i,1)+md)%md;
	}printf("%lld\n",as);
} 