#include<bits/stdc++.h> 
using namespace std;
namespace Ruri{
    #define ms(a,b) memset(a,b,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back 
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second                         
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=1e9;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
    #define ss(a) scanf("%s",a)
    inline int ri(){ int x; scanf("%d",&x); return x; }
    inline ll rl(){ ll x;  scanf("%lld",&x); return x; }
    inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
    #define pr(x) cout<<#x<<": "<<(x)<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");   
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
const int MAX_N=605;
const int OFFSET=MAX_N*MAX_N/2;
ll n,mod;
ll ans[MAX_N];
ll g[2][OFFSET<<1],s[2][OFFSET<<1];
int main()
{
	n=rl(),mod=rl();
	g[0][OFFSET]=1;
	repi(i,OFFSET,(OFFSET>>1)-1) s[0][i]=1;
	repi(i,1,n){
		int now=i&1,last=now^1;
		ms(g[now],0),ms(s[now],0);
		int mx=i*(i-1)/2;
		g[now][OFFSET-mx]=1;
		repi(j,OFFSET-mx+1,OFFSET+mx){
			g[now][j]=(g[now][j-1]+(s[last][j+i-1]-s[last][j-1])-(s[last][j-1]-s[last][j-i-1]));
			g[now][j]=(g[now][j]%mod+mod)%mod;
		}
		mx=(i+1)*(i+2)/2;
		repi(j,OFFSET-mx,OFFSET+mx) s[now][j]=(g[now][j]+s[now][j-1])%mod;
		repi(j,1,i-1) ans[i]=(ans[i]+(s[last][OFFSET+(i+1)*i/2]-s[last][OFFSET+j])%mod*(i-j)%mod+mod)%mod;
		ans[i]=(ans[i]+1ll*i*ans[i-1]%mod)%mod;
	}
	printf("%lld\n",pos_mod(ans[n],mod));
    return 0;
}