就是用树状数组优化了,然后根据公式搞,然后就WA。
//2021.3.19
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,ans,fac[1000010],tree[1000010];
const ll mod=998244353;
ll lowbit(ll x){
return x&-x;
}
void add(ll id,ll val){
for(;id<=n;id+=lowbit(id)) tree[id]+=val;
}
ll ask(ll id){
ll res=0;
for(;id;id-=lowbit(id)) res+=tree[id];
return res;
}
void Fac(){
fac[0]=1;
for(ll i=2;i<=n;i++){
fac[i]=fac[i-1]*i%mod;
add(i,1);
}
}
int main(){
scanf("%lld",&n);
Fac();
for(ll i=1;i<=n;i++){
ll x;
scanf("%lld",&x);
ans=(ans+(ask(x)-1)*fac[n-i])%mod;
add(x,-1);
}
printf("%lld",ans+1);
return 0;
}
求助,谢谢