#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int mod = 1e9 + 7;
struct SegmentTree {
int n;
vector<int> t;
SegmentTree(int _n) {
n = _n;
t.resize(4 * n);
}
int merge(int x, int y) {
return x + y;
}
void update(int id, int tl, int tr, int i, int v) {
if (tl == tr) {
t[id] = v;
return;
}
int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
if (i <= tm) {
update(x, tl, tm, i, v);
} else {
update(y, tm + 1, tr, i, v);
}
t[id] = merge(t[x], t[y]);
}
int get(int id, int tl, int tr, int l, int r) {
if (r < tl || tr < l) return 0LL;
if (l <= tl && tr <= r) return t[id];
int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
return merge(get(x, tl, tm, l, r), get(y, tm + 1, tr, l, r));
}
void update(int i, int v) { update(0, 0, n - 1, i, v); }
int get(int l, int r) { return get(0, 0, n - 1, l, r); }
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto b = a;
ranges::sort(b);
for (auto &x : a) {
x = ranges::lower_bound(b, x) - b.begin() + 1;
}
SegmentTree t(n + 1);
for (int i = 1; i <= n; i++) {
t.update(i, 1);
}
vector<int> f(n + 1);
f[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1] * i % mod;
}
int ans = 1;
for (int i = 0; i < n; i++) {
int cnt = t.get(1, a[i] - 1);
ans = (ans + cnt * f[n - i - 1]) % mod;
t.update(a[i], 0);
}
cout << ans << '\n';
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... |