Problem: This is the hard version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved.

A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it.

Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j.

Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions:

  * p is lexicographically smaller than q. 
  * the number of inversions in p is greater than the number of inversions in q. 



Print the number of such pairs modulo mod. Note that mod may not be a prime.

Input

The only line contains two integers n and mod (1≤ n≤ 500, 1≤ mod≤ 10^9).

Output

Print one integer, which is the answer modulo mod.

Example

Input


4 403458273


Output


17

Note

The following are all valid pairs (p,q) when n=4.

  * p=[1,3,4,2], q=[2,1,3,4], 
  * p=[1,4,2,3], q=[2,1,3,4], 
  * p=[1,4,3,2], q=[2,1,3,4], 
  * p=[1,4,3,2], q=[2,1,4,3], 
  * p=[1,4,3,2], q=[2,3,1,4], 
  * p=[1,4,3,2], q=[3,1,2,4], 
  * p=[2,3,4,1], q=[3,1,2,4], 
  * p=[2,4,1,3], q=[3,1,2,4], 
  * p=[2,4,3,1], q=[3,1,2,4], 
  * p=[2,4,3,1], q=[3,1,4,2], 
  * p=[2,4,3,1], q=[3,2,1,4], 
  * p=[2,4,3,1], q=[4,1,2,3], 
  * p=[3,2,4,1], q=[4,1,2,3], 
  * p=[3,4,1,2], q=[4,1,2,3], 
  * p=[3,4,2,1], q=[4,1,2,3], 
  * p=[3,4,2,1], q=[4,1,3,2], 
  * p=[3,4,2,1], q=[4,2,1,3]. 

Solution (in CPP):

Solution:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

inline ll read(){
    ll Hashimoto=0;
	bool Kanna=1;
    char I_Love=getchar();
    while(I_Love<'0'||I_Love>'9'){
        if(I_Love=='-')Kanna=0;
        I_Love=getchar();
    }
    while(I_Love>='0'&&I_Love<='9'){
        Hashimoto=Hashimoto*10+I_Love-'0';
        I_Love=getchar();
    }
    return (Kanna?Hashimoto:-Hashimoto);
}
template<typename T1,typename T2> inline void Umax(T1 &a,T2 b){
	if(a<b)a=b;
}
template<typename T1,typename T2> inline void Umin(T1 &a,T2 b){
	if(a>b)a=b;
}

#define YoRHa main
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define filein(s) freopen(s,"r",stdin);
#define fileout(s) freopen(s,"w",stdout);
#define file freopen("I_Love_Hashimoto_Kanna.out","w",stdout);
#define RE cout<<"I_Love_Hashimoto_Kanna"<<endl;
#define Tone(Kanna) cout<<Kanna<<endl;
#define Tall(Hashimoto,Kanna) for(int I_Love=0;I_Love<(Kanna);++I_Love)cout<<Hashimoto[I_Love]<<(I_Love==(Kanna)-1?"\n":" ");
#define FOR(I_Love,Hashimoto,Kanna) for(int I_Love=Hashimoto;I_Love<(Kanna);++I_Love)
#define MP(Hashimoto,Kanna) make_pair(Hashimoto,Kanna)
#define REV(Kanna) reverse(Kanna.begin(),Kanna.end());
#define SORT(Kanna) sort(Kanna.begin(),Kanna.end());
#define UNIQUE(Kanna) Kanna.erase(unique(Kanna.begin(),Kanna.end()),Kanna.end());
#define inf (1000000000)
#define linf (1000000000000000000ll)
#define MOD(x) if(x>=mod)x-=mod;

int n;
ll mod;
ll dp[555];
ll num[2][308888]={};
ll pre[308888];
ll ppre[308888];
ll nump[308888];

int YoRHa() {
	//完全想清楚了再开始写。
	//写不顺、不知道怎么写、很乱的时候，停下来好好想想。
	//做得慢总比做不出好。
	fastio;
	cin>>n;
	cin>>mod;
	dp[1]=0;
	num[0][0]=1;
	int cur=0,nxt=1;
	FOR(i,1,n){
		memset(pre,0,sizeof(pre));
		memset(ppre,0,sizeof(ppre));
		memset(num[nxt],0,sizeof(num[nxt]));
		ll fuck=0;
		FOR(j,1,i*i){
			fuck+=num[cur][j-1];
			MOD(fuck);
			pre[j]=pre[j-1]+fuck;//pre[j]:<j的 
			MOD(pre[j]);
			ppre[j]=ppre[j-1]+pre[j];
			MOD(ppre[j]);
		}
		memset(nump,0,sizeof(nump));
		FOR(j,0,300000){
			nump[j+1]=nump[j]+num[cur][j];
			MOD(nump[j+1]);
		}
//		FOR(j,0,i*i)FOR(k,0,i+1){
//			num[nxt][j+k]+=num[cur][j];
//			MOD(num[nxt][j+k]);
//		}
		FOR(j,0,300000){
			num[nxt][j]=nump[j+1]-(j-i<0?0:nump[j-i])+mod;
			MOD(num[nxt][j]);
		}
		dp[i+1]=dp[i]*(i+1)%mod;
//		FOR(j,2,i*i)FOR(k,0,i){//k:目前的第几组；j:目前第几种 
//			if(j+k-i<0){
//				dp[i+1]+=num[cur][j]*pre[j-1]%mod;
//				MOD(dp[i+1]);
//			}
//			else {
//				dp[i+1]=(dp[i+1]+num[cur][j]*(pre[j-1]-pre[j+k-i-1]+mod))%mod;
//				
//			}
//		}
		FOR(j,2,i*i){
			if(j-i>=2){
				dp[i+1]=(dp[i+1]+num[cur][j]*(i*pre[j-1]%mod-ppre[j-2]+ppre[j-i-2]+mod)%mod)%mod;
			}
			else {
				dp[i+1]=(dp[i+1]+num[cur][j]*(i*pre[j-1]%mod-ppre[j-2]+mod)%mod)%mod;
			}
		}
		
		
		swap(cur,nxt);
	}
	cout<<dp[n]<<endl;
	
	
	return 0;
}