#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << " = " << x << endl
#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define per(i, a, b) for(int i = (a); i >= (b); -- i)
#define MP make_pair
#define pb push_back
#define SIZ(x) ((int)(x).size())
#define fi first
#define se second
#define fill(a, b) memset(a, b, sizeof(a))
typedef long long LL;
typedef unsigned long long u64;
typedef unsigned int u32;
template <typename Tp> void read(Tp &x) {
	x = 0; int op = 1; char ch = getchar();
	while(!isdigit(ch)) { if(ch == '-') op = -1; ch = getchar(); }
	while(isdigit(ch)) { x = x*10+ch-'0'; ch = getchar(); } x *= op;
}
template <typename Tp> void CMax(Tp &x, Tp y) { if(y > x) x = y; }
template <typename Tp> void CMin(Tp &x, Tp y) { if(y < x) x = y; }

const int N = 501;
const int Lim = N * N / 2;
int MOD;
int Pls(int x, int y) {
	x += y;
	if(x >= MOD) x -= MOD;
	return x;
}
int Sub(int x, int y) {
	x -= y;
	if(x < 0) x += MOD;
	return x;
}

int n;
int C[N][N], fac[N];
int f[2][N * N];
int sf[2][N * N];

void process() {
	C[0][0] = 1;
	rep(i, 1, n) {
		C[i][0] = 1;
		rep(j, 1, n) C[i][j] = Pls(C[i - 1][j], C[i - 1][j - 1]);
	}
	fac[0] = 1;
	rep(i, 1, n) fac[i] = 1ll * fac[i - 1] * i % MOD;
}

int ans;

int main()
{
	read(n); read(MOD);
	process();
	int cr = 1, pr = 0;
	f[cr][Lim] = 1;
	sf[cr][Lim] = sf[cr][Lim + 1] = 1;
	per(i, n - 2, 1) {
		cr ^= 1; pr ^= 1;
		int l = n - i;
		int tot = l * (l - 1) / 2;
		int cur = 0;
		rep(j, Lim - tot, Lim + tot) {
			cur = Pls(cur, 1ll * f[pr][j] * l % MOD);
			f[cr][j] = cur;
			if(j - l >= Lim - tot) cur = Sub(cur, Sub(sf[pr][j], sf[pr][j - l]));
			else cur = Sub(cur, sf[pr][j]);
		}
		cur = 0;
		per(j, Lim + tot, Lim - tot) {
			f[cr][j] = Pls(f[cr][j], cur);
			cur = Pls(cur, 1ll * f[pr][j] * l % MOD);
			if(j + l - 1 <= Lim + tot) cur = Sub(cur, Sub(sf[pr][j + l - 1], sf[pr][j - 1]));
			else cur = Sub(cur, Sub(sf[pr][Lim + tot], sf[pr][j - 1]));
		}
		sf[cr][Lim - tot - 1] = 0;
		int ntot = (l + 1) * l / 2;
		rep(j, Lim - ntot, Lim + ntot) {
			sf[cr][j] = Pls(sf[cr][j - 1], f[cr][j]);
		}
		int res = 0;
		rep(d, 1, l) {
			res = Pls(res, 1ll * (l + 1 - d) * Sub(sf[cr][Lim + tot], sf[cr][Lim + d]) % MOD);
		}
		ans = Pls(ans, 1ll * res * fac[i - 1] % MOD * C[n][i - 1] % MOD);
	}
	printf("%d", ans);
	return 0;
}
