Problem: This is the hard version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved.

A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it.

Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j.

Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions:

  * p is lexicographically smaller than q. 
  * the number of inversions in p is greater than the number of inversions in q. 



Print the number of such pairs modulo mod. Note that mod may not be a prime.

Input

The only line contains two integers n and mod (1≤ n≤ 500, 1≤ mod≤ 10^9).

Output

Print one integer, which is the answer modulo mod.

Example

Input


4 403458273


Output


17

Note

The following are all valid pairs (p,q) when n=4.

  * p=[1,3,4,2], q=[2,1,3,4], 
  * p=[1,4,2,3], q=[2,1,3,4], 
  * p=[1,4,3,2], q=[2,1,3,4], 
  * p=[1,4,3,2], q=[2,1,4,3], 
  * p=[1,4,3,2], q=[2,3,1,4], 
  * p=[1,4,3,2], q=[3,1,2,4], 
  * p=[2,3,4,1], q=[3,1,2,4], 
  * p=[2,4,1,3], q=[3,1,2,4], 
  * p=[2,4,3,1], q=[3,1,2,4], 
  * p=[2,4,3,1], q=[3,1,4,2], 
  * p=[2,4,3,1], q=[3,2,1,4], 
  * p=[2,4,3,1], q=[4,1,2,3], 
  * p=[3,2,4,1], q=[4,1,2,3], 
  * p=[3,4,1,2], q=[4,1,2,3], 
  * p=[3,4,2,1], q=[4,1,2,3], 
  * p=[3,4,2,1], q=[4,1,3,2], 
  * p=[3,4,2,1], q=[4,2,1,3]. 

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define LD long double

const int N = 510;

int inf = 1e9;
int mod = 1e9 + 7;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)


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;
}

inline int mul(int a, int b) {
  return (int) ((long long) a * b % mod);
}

inline int power(int a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) {
  a %= mod;
  if (a < 0) a += mod;
  int b = mod, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += mod;
  return u;
}

int ncr[N][N];
int fact[N];

void pre() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= i; j++) {
            if (!j) ncr[i][j] = 1;
            else {
                add(ncr[i][j], ncr[i - 1][j - 1]);
                add(ncr[i][j], ncr[i - 1][j]);
            }
        }
    }
    fact[0] = 1;
    for (int i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

int dp[2][2 * N * N];
int pdp[2][2 * N * N];

/** To check Time Limit **/
class Timer {
private:
    using clock_t = std::chrono::high_resolution_clock;
    using second_t = std::chrono::duration<double, std::ratio<1> >;
    std::chrono::time_point<clock_t> m_beg;
public:
    Timer() : m_beg(clock_t::now()) {}
    void reset() { m_beg = clock_t::now(); }
    double elapsed() const {
        return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
    }
}T;
/** END **/

signed main()
{
    //freopen("IN", "r", stdin);
    //freopen("OUT", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

//    T.reset();

    int n, m;
    cin >> n >> m;

    mod = m;

    pre();

    int ans = 0;
    int cur = 0;

    int LIM = n * (n - 1) / 2;
    
    int offset = LIM;

    dp[0][0 + offset] = 1;

    for (int j = -LIM; j <= LIM; j++) {
        pdp[0][j + offset] = mul(j + offset, dp[0][j + offset]);
        if (j > -LIM) add(pdp[0][j + offset], pdp[0][j - 1 + offset]);
    }

    for (int j = LIM; j >= -LIM; j--) {
        if (j + 1 <= LIM)
            add(dp[0][j + offset], dp[0][j + 1 + offset]);
    }

    for (int i = 1; i < n; i++) {
        cur ^= 1;
        int lim = n * (n - 1) / 2;
        int lim1 = i * (i - 1) / 2;
//        int lim1 = lim;
        for (int j = -lim1; j <= lim1; j++) {
            int r = max(j - i, -lim);

            int temp1 = pdp[cur ^ 1][j + offset];
            if (r > -lim) sub(temp1, pdp[cur ^ 1][r - 1 + offset]);

            int temp2 = dp[cur ^ 1][r + offset];
            if (j + 1 <= lim) sub(temp2, dp[cur ^ 1][j + 1 + offset]);

            sub(temp1, mul((r + offset), temp2));

            r = min(j + i, lim);

            int temp3 = pdp[cur ^ 1][r + offset];
            if (j + 1 > -lim) sub(temp3, pdp[cur ^ 1][j + offset]);

            int temp4 = dp[cur ^ 1][j + 1 + offset];
            if (r + 1 <= lim) sub(temp4, dp[cur ^ 1][r + 1 + offset]);

            sub(temp3, mul((j + offset), temp4));
            temp3 = mul(temp3, mod - 1);
            temp4 = mul(i, temp4);
            add(temp3, temp4);

            int T = 0;
            add(T, temp1);
            add(T, temp3);
            dp[cur][j + offset] = T;

//            debug(i, j, dp[cur][offset]);
        }

        for (int j = -lim1; j <= lim; j++) {
            if (j > lim1) dp[cur][j + offset] = 0;
            pdp[cur][j + offset] = mul(j + offset, dp[cur][j + offset]);
            if (j > -lim) add(pdp[cur][j + offset], pdp[cur][j - 1 + offset]);
        }

        for (int j = lim1; j >= -lim; j--) {
            if (j < -lim1) dp[cur][j + offset] = 0;
            if (j + 1 <= lim)
                add(dp[cur][j + offset], dp[cur][j + 1 + offset]);
        }

        int temp1 = mul(ncr[n][n - i - 1], fact[n - i - 1]);
        int temp2 = 0;

        for (int j = 1; j <= i + 1; j++) {
            for (int k = j + 1; k <= i + 1; k++) {
//                debug(i, n - i, j, k, dp[cur][1 + k - j + offset]);
                add(temp2, dp[cur][1 + k - j + offset]);
            }
        }

//        debug(temp1, temp2);

        add(ans, mul(temp1, temp2));
    }

    cout << ans << "\n";

//    cout << T.elapsed() << "\n";

    return 0;
}