Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l,r], where l,r are two integers with 1≤l≤r≤n. This indicates the subsegment where l−1 elements from the beginning and n−r elements from the end are deleted from the sequence.
For a permutation p1,p2,…,pn, we define a framed segment as a subsegment [l,r] where max{pl,pl+1,…,pr}−min{pl,pl+1,…,pr}=r−l. For example, for the permutation (6,7,1,8,5,3,2,4) some of its framed segments are: [1,2],[5,8],[6,7],[3,3],[8,8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l,r) such that 1≤l≤r≤n, and [l,r] is a framed segment. For example, the permutation [3,1,2] has happiness 5: all segments except [1,2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1≤n≤250000, 108≤m≤109, m is prime).
Output
Print r (0≤r<m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
input
1 993244853
output
1
input
2 993244853
output
6
input
993244853
output
32
input
2019 993244853
output
923958830
input
2020 437122297
output
265955509
Note
For sample input n=3, let’s consider all permutations of length 3:
[1,2,3], all subsegments are framed segment. Happiness is 6.
[1,3,2], all subsegments except [1,2] are framed segment. Happiness is 5.
[2,1,3], all subsegments except [2,3] are framed segment. Happiness is 5.
[2,3,1], all subsegments except [2,3] are framed segment. Happiness is 5.
[3,1,2], all subsegments except [1,2] are framed segment. Happiness is 5.
[3,2,1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6=32.
题意
长度为n的序列,序列中的值1~n,且不重复。问全排列中子段l,r,满足max-min=r-l的个数
思路
我们从子段的角度考虑,子段长度为len时,此时个数为len * len! * (n - len +1)!
然后我们求和就行。
#include <bits/stdc++.h>
typedef long long ll;
ll n,mod;
using namespace std;
namespace fastIO {
inline void input(ll& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 1e6+5;
int Case;
ll f[N];
void init(){
f[0]=1;
for(int i=1;i<N;i++) f[i]=f[i-1]*i%mod;
}
int main(){
Case=1;
//input(Case);
while(Case--){
input(n);input(mod);
init();
ll ans = 0;
for(int i=1;i<=n;i++){
ans += i*f[n-i+1]%mod*f[i]%mod;
ans%=mod;
}
printf("%lld",ans);
}
return 0;
}
/*
2020 437122297
*/