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;
#define ll long long
#define pii pair<ll,ll>
#define pb push_back
#define fi first
#define se second
const ll N = 3e5+3;
const ll MOD = 1e9+7;
ll a[N],b[N],fac[N],bit[N];
ll n,tmp,ans;
map<ll,ll> mp;
void update(ll idx) {
for(int i=idx; i<=n; i+=i&-i) {
bit[i]++;
}
}
ll query(ll idx) {
ll ret = 0;
for(int i=idx; i>=1; i-=i&-i) {
ret += bit[i];
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
fac[0] = 1;
for(int i=1; i<=n; i++) {
cin >> a[i];
b[i] = a[i];
fac[i] = fac[i-1] * i;
fac[i] %= MOD;
}
sort(b+1,b+n+1);
for(int i=1; i<=n; i++) {
mp[b[i]] = i;
}
for(int i=1; i<=n; i++) a[i] = mp[a[i]];
for(int i=n; i>=1; i--) {
ans += (fac[n-i] * query(a[i]-1)) % MOD;
ans %= MOD;
update(a[i]);
}
cout << (ans + 1) % MOD << endl;
}
# | 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... |