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

#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define si(c) ((int)(c).size())
#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)
#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << (a) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T>
vector<T> make_unique(vector<T> a) {
    sort(all(a));
    a.erase(unique(all(a)), a.end());
    return a;
}

int MOD;

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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    int n; cin >> n >> MOD;
    vi p(n*n); p[0] = 1 % MOD;
    int deg = 0;

    ll ans = 0;
    for (int len = 1; len <= n; len++) {
        ans = ans * len % MOD;
        int zero = deg/2;

        vi cum(deg+1);
        forn(i, deg+1) cum[i] = p[i];
        dforsn(i,0,deg) add(cum[i], cum[i+1]);
        
        forn(j,len) forn(i,j) {
            auto d = j-i;
            if (zero+d+1 < si(cum)) {
                ans += cum[zero+d+1];
                ans %= MOD;
            }
        }

        forn(it, 2) {
            for (int i = deg; i >= 0; i--) 
                add(p[i+len], MOD - p[i]);
            forn(i, deg + len) add(p[i+1], p[i]);
            deg += len-1;
        }
    }
    cout << ans % MOD << endl;
    return 0;
}
