import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def fastfrac(a,b,M):
numb = pow(b,M-2,M)
return ((a%M)*(numb%M))%M
def comb(n,k,M):
if n<k: return 0
if n==k: return 1
if (n,k) in storecomb: return storecomb[(n,k)]
num1 = factor[n]
num2 = factor[k]
num3 = factor[n-k]
num = (num2*num3)%M
output = fastfrac(num1,num)
storecomb[(n,k)] = output
return output
def main(t):
n,M = map(int,input().split())
factor = [1]
for i in range(1,n+1): factor.append(factor[-1]*(i)%M)
def fastfrac(a,b,M):
numb = pow(b,M-2,M)
return ((a%M)*(numb%M))%M
comb = [[0 for _ in range(n+2)] for _ in range(n+1)]
comb[0][0] = 1
for i in range(1,n+1):
for j in range(i+1):
comb[i][j] = (comb[i-1][j-1] + comb[i-1][j])% M
ans = 0
for i in range(n//2,n-1):
for j in range(n-1-i):
num1 = comb[n-2-i][j]
num2 = i-(i-n//2)*2
num3 = factor[i+j-1]
ans += (num1*num2)%M * num3 % M
ans %= M
if n%2==0: ans += factor[n-2]
print(ans*n%M)
T = 1 t = 1
while t<=T:
main(t)
t += 1
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e4;
int MOD;
int n,m,fac[MAXN + 5],inv[MAXN + 5];
int qpow(int a,int n){
int ret = 1;
while(n){
if(n & 1)ret = 1ll * ret * a % MOD;
a = 1ll * a * a % MOD;
n >>= 1;
}
return ret;
}
int c(int n,int m){
if(m > n)return 0;
return 1ll * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
int main(){
scanf("%d%d",&n,&MOD);
fac[0] = 1;
inv[0] = 1;
for(int i = 1; i <= MAXN; i++){
fac[i] = 1ll * fac[i - 1] * i % MOD;
inv[i] = qpow(fac[i],MOD - 2);
}
long long ans = 0;
for (int i = n / 2; i < n; i++){
int x = n / 2 * 2 - i;
for (int j = 0; j <= max(0, n - i - 2); j++){
ans = (ans + 1ll * n * x % MOD * c(max(0, n - i - 2),j) % MOD * fac[i + j - 1] % MOD) % MOD;
}
}
cout << ans;
}
1618A - Polycarp and Sums of Subsequences | 1618B - Missing Bigram |
938. Range Sum of BST | 147. Insertion Sort List |
310. Minimum Height Trees | 2110. Number of Smooth Descent Periods of a Stock |
2109. Adding Spaces to a String | 2108. Find First Palindromic String in the Array |
394. Decode String | 902. Numbers At Most N Given Digit Set |
221. Maximal Square | 1200. Minimum Absolute Difference |
1619B - Squares and Cubes | 1619A - Square String |
1629B - GCD Arrays | 1629A - Download More RAM |
1629C - Meximum Array | 1629D - Peculiar Movie Preferences |
1629E - Grid Xor | 1629F1 - Game on Sum (Easy Version) |
2148. Count Elements With Strictly Smaller and Greater Elements | 2149. Rearrange Array Elements by Sign |
2150. Find All Lonely Numbers in the Array | 2151. Maximum Good People Based on Statements |
2144. Minimum Cost of Buying Candies With Discount | Non empty subsets |
1630A - And Matching | 1630B - Range and Partition |
1630C - Paint the Middle | 1630D - Flipping Range |