#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<=0;i--) if(tv+bit[ent+(1< V) { static BIT bt; ZERO(bt.bit); ZERO(bt.val); int x=0,i; map M; vector V2; M[-1LL<<60]=0; FORR(r,V) M[r]=0; FORR(r,M) r.second=x++; ll ret=0; for(i=V.size()-1;i>=0;i--) { ret += bt.total(M[V[i]]); bt.add(M[V[i]],1); } return ret; } int N; ll K; vector P; ll fact[101010], totinv[101010]; ll ret; ll mo=1000000007; ll getsmaller(int n) { static BIT bt; ZERO(bt.bit); ZERO(bt.val); int i; ll tot=0; for(i=n+1;i v; ll r=0; int i; FOR(i,n) v.push_back(i); while(k--) { r+=array_inv(v)%mo; next_permutation(v.begin(),v.end()); } return r%mo; } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K; if(K==0) return _P("0\n"); P.push_back(0); FOR(i,N) cin>>x, P.push_back(x); N++; fact[0]=1; totinv[0]=0; for(i=1;i<=N;i++) { fact[i]=fact[i-1]*i; if(fact[i]=0;i--) { if(fact[N-1-i]>=K) { out: ret += factinv(N-1-i,K); break; } if(P[i]>P[i+1]) { ll smaller=getsmaller(i); for(x=i;x