#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
typedef long long ll;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long t,n,m,k,ccnt,pos=0,sum=0,minn2=INT_MAX,sum2,cnt=0,cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0,cnt7=0,a[1000005],x1,y7,x2,y2,x3,y3,bbb;
long long b[1000005],c[1000005],x,y,z,d=0,l2,r2,temcnt,le[200005],ri[200005];
//long long c3[2005][2005],c4[1000005],kk[1000005];
long long pos1=0,pos2=0,tem,tem2,l,r,ans=0,mod=998244353,tw[100005],th[100005],ee,loc[1000005],te[100005],vis[1000005],ans1[100005],ans2[100005];
long long minn=LONG_LONG_MAX,maxx=-LONG_LONG_MAX,maxx2,posb,mid,mxl[100005],mxr[100005],mnl[100005],mnr[100005],mx[100005],mn[100005],plmx[100005],prmx[100005],plmn[100005],prmn[100005];
ll c10,c11,c20,c21,dp[505][505][2],dp2[505][505][2],prec[1000000];
long double dd[10005],aaa,nn;
string s1,s2,s;
char ch,ch2,cc[1005][1005],cc2[1005][1005];
vector<ll> v[1000005];
pair<ll,ll> p[505];
map<pair<ll,ll>,ll> mp;
long long fpow(long long aa,long long bb)
{
    if(bb==0)
    {
        return 1;
    }
    else if(bb<0)
    {
        return 0;
    }
    long long kk1=aa;
    long long kk2=1;
    while(bb>1)
    {
        if(bb%2)
        {
            kk2*=kk1;
            kk2%=mod;
        }
        kk1*=kk1;
        kk1%=mod;
        bb=bb/2;
    }
    return (kk1*kk2)%mod;
}
long long co(long long x,long long y)
{
    long long ans=1;
    long long ans2=1;
    if(x==y||y==0)
    {
        return 1;
    }
    else
    {
        for(int i=x-y+1;i<=x;i++)
        {
            ans=ans*i;
            ans=ans%mod;
        }
        for(int i=1;i<=y;i++)
        {
            ans2=ans2*i;
            ans2=ans2%mod;
        }
        ans2=fpow(ans2,mod-2);
        ans2=(ans2*ans)%mod;
        return ans2;
    }
}
int summ(ll x)
{
    //cout<<"! "<<x<<'\n';
    ll ans=0,pos=0;
    ll maxx;
    x++;
    ll xx=x;

    for(int i=0;;i++)
    {
        if(tw[i]>x)
        {
            maxx=i;
            break;
        }
    }
    //cout<<"???????? "<<maxx<<'\n';
    for(int i=maxx-1;i>=0;i--)
    {
        if(xx>=tw[i])
        {
            th[i]=1;
            xx-=tw[i];
        }
        else
        {
            th[i]=0;
        }
    }
    /*for(int i=maxx-1;i>=0;i--)
    {
        cout<<th[i];
    }
    cout<<'\n';*/
    for(int i=maxx-1;i>=0;i--)
    {
        if(th[i])
        {
            ans+=v[i][pos];
            pos=2*pos+2;
        }
        else
        {
            pos*=2;
        }
    }
    return ans;
}
void modify(ll x,ll m)
{
    v[0][x]+=m;
    for(int i=1;i<cnt2;i++)
    {
        v[i][x/2]+=m;
        x/=2;
    }
}
void dfs(ll y)
{
    //cout<<y<<" ";
    a[y]=cnt;
    vis[y]=1;
    for(int i=0;i<v[y].size();i++)
    {
        if(vis[v[y][i]]==0)
        {
            //dp[x][v[y][i]]=dp[x][y]+1;
            dfs(v[y][i]);
        }
    }
}
signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    //cout<<fixed<<setprecision(20);
    cin>>n>>mod;
    if(n<=3)
    {
        cout<<0;
    }
    else
    {
        a[0]=1;
        a[1]=2;
        a[2]=2;
        a[3]=1;
        b[3]=0;
        for(int i=4;i<=n;i++)
        {
            b[i]=b[i-1]*i;
            b[i]%=mod;
            ans1[0]=1;
            if(mod==1)
            {
                ans1[0]=0;
            }
            cnt1=(i-1)*(i-2)/2;
            cnt2=i*(i-1)/2;
            for(int j=1;j<=cnt1;j++)
            {
                ans1[j]=ans1[j-1]+a[j];
                ans1[j]%=mod;
            }
            c[0]=ans1[0];
            for(int j=1;j<=cnt1;j++)
            {
                c[j]=c[j-1]+ans1[j];
                c[j]%=mod;
            }
            prec[0]=c[0];
            for(int j=1;j<=cnt1;j++)
            {
                prec[j]=prec[j-1]+c[j];
                prec[j]%=mod;
            }
            for(int j=2;j<=cnt1;j++)
            {
                if(j==2)
                {
                    //cout<<j<<" "<<a[j]<<" "<<c[0]*(i-1)<<'\n';
                    b[i]+=(a[j]*c[0]*(i-1));
                    b[i]%=mod;
                }
                else
                {
                    y=j-i-1;
                    if(y<=0)
                    {
                        z=c[j-2]*(i-1)-(prec[j-3]);
                    }
                    else
                    {
                        z=c[j-2]*(i-1)-(prec[j-3]-prec[y-1]);
                    }

                    //cout<<j<<" "<<a[j]<<" "<<z<<'\n';
                    z%=mod;
                    z*=a[j];
                    z%=mod;
                    if(z<0)
                    {
                        z+=mod;
                    }
                    b[i]+=z;
                    b[i]%=mod;
                }

            }
            for(int j=0;j<=i*(i-1)/2;j++)
            {
                x=j-i+1;
                y=j;
                if(x<0)
                {
                    x=0;
                }
                if(y>cnt1)
                {
                    y=cnt1;
                }
                if(x==0)
                {
                    a[j]=ans1[y];
                }
                else
                {
                    a[j]=(ans1[y]-ans1[x-1]+mod)%mod;
                }
                //cout<<a[j]<<" ";
            }
            //cout<<'\n';
        }
        cout<<b[n];
    }



}
