/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \
      |             |       |\          |    |       |       |\          |
   \  |             |       | \         |    |       |       | \         |
    \ |             |       |  \        |    |        \     /   \        |
     V              |       |   \        \__/|         -----     \       |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
	cerr << "\e[1;33m" << s << " = [";
	while(l != r) {
		cerr << *l;
		cerr << (++l == r ? ']' : ',');
	}
	cerr << "\e[0m\n";
}

#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS  = 1e-8;
const int INF     = 0x3F3F3F3F;
const ll LINF     = 4611686018427387903;
const int MOD     = 1e9+7;
/*--------------------------------------------------------------------------------------*/

int mod;
inline void add(int &a, int b) {
	a += b;
	if(a >= mod)
		a -= mod;
}
inline void sub(int &a, int b) {
	a -= b;
	if(a < 0)
		a += mod;
}

signed main() { EmiliaMyWife
	int n;
	cin >> n >> mod;
	int ans = 0;

	vector<vector<int>> dp(n + 1, vector<int>(n * (n - 1) / 2 + 1));
	dp[0][0] = 1;
	for(int i = 1; i <= n; i++) {
		int s = 0;
		for(int j = 0; j <= n * (n - 1) / 2; j++) {
			add(s, dp[i - 1][j]);
			if(j - i >= 0)
				sub(s, dp[i - 1][j - i]);
			dp[i][j] = s;
		}
	}

	ll g = 1;
	for(int i = 1; i < n; i++) {
		int sum = 0, sum2 = 0, sum3 = 0;
		for(int j = 0; j <= (n - i) * (n - i - 1) / 2; j++) {
			add(ans, 1LL * sum * dp[n - i][j] % mod * g % mod);

			if(j - 1 >= 0)
				add(sum2, dp[n - i][j - 1] * 1LL * (n - i) % mod);

			if(j - 2 >= 0) {
				add(sum3, dp[n - i][j - 2]);
				if(j - 2 - (n - i) >= 0)
					sub(sum3, dp[n - i][j - 2 - (n - i)]);

				sub(sum2, sum3);
			}
			add(sum, sum2);
		}
		/*
		for(int dif = 1; dif < n - i + 1; dif++) {
			ll sum = 0, res = 0;
			for(int j = dif + 1; j <= n * (n - 1) / 2; j++) {
				sum = (sum + dp[n - i][j - dif - 1]) % mod;
				res = (res + sum * dp[n - i][j]) % mod;
			}
			ans = (ans + res * (n - i + 1 - dif) % mod * g) % mod;
		}
		*/
		g = g * (n - i + 1) % mod;
	}
	cout << ans << '\n';
}
