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 maxn = 300100, mod = 1e9 + 7;
int add(int a, int b) {
a += b;
if (a >= mod)
a -= mod;
return a;
}
int mul(int a, int b) {
return 1ll * a * b % mod;
}
int N, A[maxn], fac[maxn];
template<typename T>
struct fenwick {
T bit[maxn];
fenwick() {
fill(bit, bit + maxn, T(0));
}
void upd(int x, T val) {
for (; x < maxn; x += x & -x)
bit[x] += val;
}
T get(int x) {
T res = 0;
for (; x; x -= x & -x)
res += bit[x];
return res;
}
};
fenwick<int> T1;
int main() {
// freopen("cc.inp", "r", stdin);
// freopen("cc.out", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
fac[0] = 1;
set<int> s;
map<int, int> mp;
for (int i = 1; i <= N; ++i) {
cin >> A[i];
s.insert(A[i]);
fac[i] = mul(fac[i - 1], i);
}
int cur = 0;
for (auto it = s.begin(); it != s.end(); ++it) {
mp[*it] = ++cur;
}
int Ans = 0;
for (int i = N; i >= 1; --i) {
A[i] = mp[A[i]];
Ans = add(Ans, mul(T1.get(A[i] - 1), fac[N - i]));
T1.upd(A[i], 1);
}
cout << add(Ans, 1);
return 0;
}
# | 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... |