#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x class BIT { public: V bit[1< P,P2; static BIT bt; vector toFNS(vector v) { // factorial number system ZERO(bt.bit); vector ret; reverse(v.begin(),v.end()); FORR(r,v) ret.push_back(bt(r)), bt.add(r,1); reverse(ret.begin(),ret.end()); return ret; } ll fact[101010], totinv[101010]; void FNSinvsuminit(){ int i; ll f=1; fact[0]=1; totinv[0]=0; for(i=1;i<=100000+5;i++) { fact[i]=fact[i-1]*i; f=f*i%mo; if(fact[i] v) { ll sum=0,num=0,f=1; int i,j; for(i=v.size()-1;i>=0;i--) { sum += totinv[v.size()-1-i]*v[i]%mo; sum += f%mo*(1LL*v[i]*(v[i]-1)/2%mo)%mo; sum += v[i]*num%mo; num=(num+v[i]*f)%mo; f=f*(v.size()-i)%mo; } return sum%mo; } void solve() { int i,j,k,l,r,x,y; string s; FNSinvsuminit(); cin>>N>>K; FOR(i,N) cin>>x, P.push_back(x); P2=P=toFNS(P); // add K FOR(i,N) { ll t=K+P2[N-1-i]; P2[N-1-i]=t%(i+1); K=t/(i+1); } cout<<(mo + K%mo*totinv[N]%mo + FNSinvsum(P2) - FNSinvsum(P))%mo<