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;
const int mod = 1e9 + 7;
set<int> s;
map<int, int> mt;
int arr[300300], n, cnt = 1, fac[300300], fw[300300], ans = 1;
void update(int idx, int val){
for(int i = idx; i<=n; i+=(i&-i)) fw[i]+=val;
}
int query(int idx){
int sum = 0;
for(int i = idx; i>0; i-=(i&-i)) sum+=fw[i];
return sum;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
fac[0] = 1;
for(int i = 1; i<=n; ++i) fac[i] = (fac[i - 1]*i)%mod;
for(int i = 1; i<=n; ++i) cin >> arr[i], s.insert(arr[i]);
for(auto x:s) mt[x] = cnt++;
for(int i = 1; i<=n; ++i){
ans = (ans + fac[n - i]*(mt[arr[i]] - query(mt[arr[i]]) - 1)%mod)%mod;
update(mt[arr[i]], 1);
}
cout << ans;
}
# | 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... |