1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include<bits/stdc++.h> #define int long long using namespace std; bool Begin; const int max_n=200005,mod=998244353,iv2=499122177; inline int read(){ int x=0;bool w=0;char c=getchar(); while(c<'0' || c>'9') w|=c=='-',c=getchar(); while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return w?-x:x; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10^48); } inline int Plus(int &x,int y){return x=(x+y>=mod?x+y-mod:x+y);} inline int Minu(int &x,int y){return x=(x<y?x-y+mod:x-y);} inline int MOD(int x){return x>=mod?x-mod:x;} inline int mi(int a,int p=mod-2){ int res=1; while(p){ if(p&1) res=res*a%mod; a=a*a%mod,p>>=1; } return res; }
int n,K,P,a[max_n],ans;
struct BIT{ int a[max_n]; inline void add(int i,int x){ for(;i<=n;i+=i&-i) Plus(a[i],x); } inline int pre(int i){ int res=0; for(;i;i-=i&-i) Plus(res,a[i]); return res; } inline int ask(int l,int r){ return MOD(pre(r)-pre(l-1)+mod); } }bit1,bit2;
int mip[max_n],inv[max_n],f[max_n];
bool End; #define File "" signed main(){ n=read(),K=read(),P=(K-1)*mi(K)%mod; mip[0]=inv[0]=1; for(int i=1;i<=n;++i) mip[i]=mip[i-1]*P%mod; inv[n]=mi(mip[n]); for(int i=n-1,ip=mi(P);i;--i) inv[i]=inv[i+1]*P%mod; iota(f+K+1,f+n+1,1); for(int i=1;i<=n;++i){ a[i]=read(); ans=MOD((ans+mip[f[i]]*bit1.pre(a[i])-mip[f[i]]*bit1.ask(a[i],n)+bit2.ask(a[i],n))%mod+mod); bit1.add(a[i],iv2*inv[f[i]]%mod); bit2.add(a[i],1); } write(ans); return 0; }
|