#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int mxn = 501, mxm = 2 * mxn * mxn;
int n, m, mod;
ll dp[mxm], f[mxm];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> mod;
	m = n * n;
	
	ll ret = 0;
	dp[m] = 1;
	for(int i = 1; i <= n; i++){
		int k = i * i;
		for(int j = m - k + 1; j <= m + k; j++) f[j] = (dp[j] + f[j - 1]) % mod;
		for(int j = m - k + 1; j <= m + k; j++){
			dp[j] = (2 * mod + dp[j - 1] + (f[min(m + k, j + i - 1)] - f[j - 1]) -
				(f[j - 1] - f[max(m - k, j - i - 1)])) % mod;
		}
		(ret *= i) %= mod;
		for(int j = 1; j < i; j++) (ret += (i - j) * (mod + f[m + k] - f[m + j])) %= mod;
	}
	
	cout << ret << endl;
	
	return 0;
}