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/extc++.h>
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/priority_queue.hpp>
using namespace std;
typedef long long LL;
// typedef __int128 LL;
typedef unsigned long long ULL;
// #pragma GCC optimize(2)
// #define int long long
#define pi acos(-1)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof(a))

LL mo = 1e9 + 7;
LL INF;

int clz(int N)
{
    return N ? 32 - __builtin_clz(N) : -INF;
}
int clz(unsigned long long N)
{
    return N ? 64 - __builtin_clzll(N) : -INF;
}
// char buf[1 << 25], *p1 = buf, *p2 = buf, obuf[1 << 25], *O = obuf; // 或者用fread更难调的快读
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)

template <class T>
void print(T x)
{
    if (x < 0)
    {
        x = -x;
        putchar('-');
        // *O++ = '-';
    }
    if (x > 9)
        print(x / 10);
    putchar(x % 10 + '0');
    // *O++ = x%10 + '0'
}
// fwrite(obuf,O-obuf,1,stdout);

template <class T>
inline void qr(T &n)
{
    n = 0;
    char c = getchar();
    bool sgn = 0;

    while (c > '9' || c < '0')
    {
        if (c == '-')
            sgn ^= 1;
        c = getchar();
    }

    while (c <= '9' && c >= '0')
    {
        n = (n << 3) + (n << 1) + (c ^ 0x30);
        c = getchar();
    }

    if (sgn)
        n = -n;
}
struct range
{
    struct _iterator
    {
        LL _start;
        LL _step;
        _iterator(LL from, LL step) : _start(from), _step(step) {}
        _iterator(LL from) : _start(from), _step(1) {}
        inline bool sign(LL x) { return x < 0; }
        bool operator!=(_iterator &b) { return _start != b._start and sign(b._start - _start) == sign(_step); }
        LL operator*() { return _start; }
        _iterator &operator++()
        {
            _start += _step;
            return *this;
        }
    };
    _iterator _finish;
    _iterator _begin;
    range(LL to) : _begin(0), _finish(to) {}
    range(LL from, LL to) : _begin(from), _finish(to) {}
    range(LL from, LL to, LL step) : _begin(from, step), _finish(to, step) {}
    _iterator &begin() { return _begin; }
    _iterator &end() { return _finish; }
};
const int MX = 1e5 + 10;

int n, m, T, tot, q, t, k, L, R;

string s;
void solve()
{
    cin >> n >> mo;
    if (n <= 3)
    {
        puts("0");
        return;
    }

    LL anss = 0;

    vector<LL> dp(n * (n + 1) >> 1, 0);
    vector<LL> pd(n * (n + 1) >> 1, 0);
    vector<LL> pf(n * (n + 1) >> 1, 0);
    vector<LL> pff(n * (n + 1) >> 1, 0);
    dp[0] = 1 % mo;
    dp[1] = 2 % mo;
    dp[2] = 2 % mo;
    dp[3] = 1 % mo;
    // LL cur = 4;

    for (auto cur : range(4, n + 1))
    {
        anss = anss * cur % mo;
        LL cpref = (cur - 1) * (cur - 2) >> 1;
        pff[0] = pf[0] = dp[0];
        for (auto i : range(1, cpref + 1))
        {
            pf[i] = (pf[i - 1] + dp[i]) % mo;
            pff[i] = (pff[i - 1] + pf[i]) % mo;
        }
        LL ctr;
        LL t1 = cpref;
        LL t2 = 0;
        LL t4 = 0;
        // LL t3 = 2;
        for (auto t3 : range(1, min(t1, cur)))
        {
            t2 = (t2 + pf[t1 - t3 - 1] * (cur - t3)) % mo;
            // cerr << "t2+=" << pf[t1 - t3 - 1] << "*" << cur - t3 << " == " << t2 << endl;
        }
        t4 = t2 * dp[t1] % mo;
        while (t1 >= 3)
        {
            t1--;
            t2 -= (cur - 1) * pf[t1 - 1];
            // cerr << "t2-=" << (cur - 1) << "*" << pf[t1 - 1] << " == " << t2 << endl;
            t2 += pff[t1 - 2];
            // cerr << "[!]t2+=" << pff[t1 - 2] << " == " << t2 << endl;
            if (t1 - 1 - cur >= 0)
                t2 -= pff[t1 - 1 - cur];
            t2 %= mo;
            t2 += mo;
            t2 %= mo;
            t4 += t2 * dp[t1];
            t4 %= mo;
        }
        // for (auto t3 : range(t1 - 2, 0, -1))
        // {
        //     t2 = (t2 - (cur - 1) * pf[t3]) + pf[t3 - 1] - (t3 - cur) >= 0 ? pf[t3 - cur] : 0);
        //     t2 %= mo;
        //     t4 = (t4 + t2 * dp[t3 + 1]) % mo;
        // }
        anss = (anss + t4) % mo;

        // for (auto i : range(1, 1 + cur))
        // {
        //     ctr = cur - i;
        //     // for (auto j : range(i + 1, cur))
        //     // {
        //     for (auto k : range(cpref, -1, -1))
        //     {
        //         if (k - i - 1 < 0)
        //             break;
        //         anss = (anss + dp[k] * pf[k - i - 1] % mo * ctr) % mo;
        //     }
        //     // }
        // }
        // pd.assign(n * (n + 1) >> 1, 0);
        pd[0] = 1;
        // for (auto i : range(cur))
        // {
        for (auto j : range(1, (cur * (cur - 1) >> 1) + 1))
        {
            // A008302
            pd[j] = (pd[j - 1] + dp[j] - (j - cur >= 0 ? dp[j - cur] : 0)) % mo;
        }
        // }
        dp = pd;
    }
    cout << anss << endl;
}

signed main()
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    // qr(T);
    // cin >> T;
    // memset(&INF, 0x3f, sizeof(INF));

    // T = 1;
    // while (T--)
    solve();
    // cin >> n;
    // qr(n);
    // }
    return 0;
}

/*+
6 1
1 1 4 5 1 4
1 6

*/