#include <bits/stdc++.h>

using namespace std;

int MOD;

int modadd(int x, int y) {
  int result = x+y;
  if (result >= MOD) result -= MOD;
  return result;
}

int modmult(int x, int y) {
  return (x * int64_t(y)) % MOD;
}

int modaddto(int& x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
}

int calc_num_pairs(int n) {
  if (n == 0) {
    return 0;
  }
  if ((n % 2) == 0) {
    return modmult(n / 2, n-1);
  }
  return modmult(n, (n-1)/2);
}

int modminus(int x) {
  if (x == 0) {
    return 0;
  }
  return MOD - x;
}

int solve(int n) {
  vector<vector<int>> dp(n+1);
  dp[0] = vector<int>(1, 1);
  for (int i = 1; i <= n; ++i) {
    int m = dp[i-1].size() + i - 1;
    vector<int>(m, 0).swap(dp[i]);
    vector<int> c(m+1);
    c[0] = 0;
    for (int j = 0; j < int(dp[i-1].size()); ++j) {
      c[j+1] = modadd(c[j], dp[i-1][j]);
    }
    for (int j = int(dp[i-1].size()); j < m; ++j) {
      c[j+1] = c[j];
    }
    for (int j = 0; j < m; ++j) {
      dp[i][j] = modadd(c[j+1], modminus(c[j-min(i-1, j)]));
    }
  }
  int ans = 0;
  for (int perm_size = 2; perm_size <= n; ++perm_size) {
    ans = modmult(ans, perm_size);
    const vector<int>& inversions = dp[perm_size-1];
    int m = inversions.size();
    vector<int> suffix_sum(m);
    suffix_sum[m-1] = inversions[m-1];
    for (int i = m-2; i >= 0; --i) {
      suffix_sum[i] = modadd(suffix_sum[i+1], inversions[i]);
    }
    vector<int> suffix_prefix(m+1+perm_size);
    suffix_prefix[0] = 0;
    for (int i = 0; i < m; ++i) {
      suffix_prefix[i+1] = modadd(suffix_prefix[i], suffix_sum[i]);
    }
    for (int i = 0; i < perm_size; ++i) {
      suffix_prefix[m+1+i] = suffix_prefix[m];
    }
    for (int smaller = 0; smaller < m; ++smaller) {
      for (int diff = 1; diff < perm_size; ++diff) {
	modaddto(ans, modmult(inversions[smaller], modadd(suffix_prefix[smaller+2+diff], modminus(suffix_prefix[smaller+2]))));
      }
    }
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n >> MOD;
  cout << solve(n) << endl;
}
