#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]);
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k <= min(i-1, j); ++k) {
	if ((j-k) < int(dp[i-1].size())) {
	  modaddto(dp[i][j], dp[i-1][j-k]);
	}
      }      
    }
  }
  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]);
    }
    for (int diff = 1; diff < perm_size; ++diff) {
      int diff_count = perm_size - diff;
      int c = 0;
      for (int smaller = 0; (smaller+diff+1) < m; ++smaller) {
	modaddto(c, modmult(inversions[smaller], suffix_sum[smaller+diff+1]));
      }
      modaddto(ans, modmult(c, diff_count));
    }
  }
  return ans;
}

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