This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define all(a) a.begin(), a.end()
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll MOD=1e9+7, LIM=3e5+7;
int T[LIM], S[LIM], tr[4*LIM], N=1;
ll sil[LIM];
map<int,int>mp;
void upd(int v) {
for(v+=N; v; v/=2) --tr[v];
}
ll cnt(int l, int r) {
l+=N; r+=N;
ll ans=tr[l];
if(l!=r) ans+=tr[r];
while(l/2!=r/2) {
if(l%2==0) ans+=tr[l+1];
if(r%2==1) ans+=tr[r-1];
l/=2; r/=2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
sil[0]=1;
for(ll i=1; i<LIM; ++i) sil[i]=(sil[i-1]*i)%MOD;
int n;
cin >> n;
while(N<n) N*=2;
rep(i, n) {
cin >> T[i];
S[i]=T[i];
tr[N+i]=1;
}
for(int i=N-1; i; --i) tr[i]=tr[2*i]+tr[2*i+1];
sort(S, S+n);
rep(i, n) mp[S[i]]=i;
ll ans=1;
rep(i, n) {
ll x=cnt(0, mp[T[i]]);
upd(mp[T[i]]);
ans=(ans+sil[n-i-1]*(x-1))%MOD;
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |