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;
using ll = long long;
int v[300005], p[300005];
ll fact[300005];
int bit[300005];
void add(int p) {
for (; p < 300005; p += (p&(-p))) {
bit[p]++;
}
}
int sum(int p) {
int s = 0;
for (; p; p -= (p&(-p))) {
s += bit[p];
}
return s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("", "r", stdin);
// freopen("", "w", stdout);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> v[i];
p[i] = v[i];
if (i == 1) fact[i] = 1;
else {
fact[i] = fact[i-1] * i;
fact[i] %= (ll) (1e9 + 7);
}
}
sort(p+1, p+n+1);
ll ans = 0;
for (int i = 1; i <= n; i++) {
int pos = lower_bound(p+1, p+n+1, v[i]) - p;
ans += (fact[n-i] * (pos-1-sum(v[i])));
ans %= (ll)(1e9 + 7);
add(v[i]);
}
cout << (ans + 1) % (ll) ( 1e9 + 7);
}
# | 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... |