#include <bits/stdc++.h>

#define int long long
using namespace std;

const int MAX = 55;
int n, MOD, fact[MAX], C[MAX][MAX], f[MAX * MAX][MAX], S[MAX * MAX];
bool mark[MAX * MAX][MAX];

int dp(int cntInv, int num) {
    if (num == 0)
        return cntInv == 0;
    if (mark[cntInv][num])
        return f[cntInv][num];
    mark[cntInv][num] = true;
    for (int i = 1; i <= min(num, cntInv + 1); i++)
        f[cntInv][num] = (f[cntInv][num] + dp(cntInv - i + 1, num - 1)) % MOD;
    return f[cntInv][num];
}

int getMaxInv(int x) {
    return (x - 1) * x / 2;
}

int32_t main() {
#ifdef ACM
    freopen("input", "r", stdin);
#endif
    cin >> n >> MOD;
    C[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        C[i][0] = C[i][i] = 1;
        for (int j = 1; j < n; j++)
            C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
    }
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
        fact[i] = fact[i - 1] * i % MOD;
    int rs = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= getMaxInv(n - i); j++)
            S[j] = 0;
        for (int j = n - i; j >= 1; j--) {
            int sum = 0;
            for (int inv = j - 1; inv <= getMaxInv(n - i); inv++) {
                rs = (rs + sum * dp(inv - j + 1, n - i - 1) % MOD * C[n][i] % MOD * fact[i]) % MOD;
                sum = (sum + S[inv]) % MOD;
                S[inv] = (S[inv] + dp(inv - j + 1, n - i - 1)) % MOD;
            }
        }
    }
    cout << rs;
}