#include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <deque>
#include <string>
#include <cmath>
#include <unordered_map>
using namespace std;

long long M;
int n;

map<int, long long> dp_lex[51], dp[51]; 
//dp_lex[i][j] = number of pairs (p,q) of permutations of length i such that 
//p is lex. smaller than q, and p has exactly j more inversions than q. 

long long work(){
    dp[1][0] = 1;
    for (int i=2;i<=n;i++){
        int mn, mx;
        mn = -(i-1)*(i-2)/2;
        mx = (i-1)*(i-2)/2;
        for (int j=0;j<i;j++){
            //put '1' j spaces apart in p and q.
            // 2 .. n gives a difference of k inversions
            for (int k=mn;k<=mx;k++){
                dp[i][k-j] += dp[i-1][k] * (i-j);
                dp[i][k-j] %= M;
                if (j != 0) {
                    dp[i][k+j] += dp[i-1][k] * (i-j);
                    dp[i][k+j] %= M;
                }

                if (k-j<= 0) continue;
                if (j > 0){
                    dp_lex[i][k-j] += dp[i-1][k] * (i-j);
                    dp_lex[i][k-j] %= M;
                }
                if (j == 0){
                    dp_lex[i][k] += dp_lex[i-1][k] * i;
                    dp_lex[i][k] %= M;

                }
            }
        }

    }
    long long ans = 0;
    for (int i=1;i<=n*(n-1)/2;i++){
        ans += dp_lex[n][i];
        if (ans >= M) ans -= M;
    }
    return ans%M;
}

int main(){
    cin >> n >> M;
    cout << work() << endl;

}

