Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long long mod = 1e9 + 7;
- int f(int i) {
- return i & (i + 1);
- }
- int g(int i) {
- return i | (i + 1);
- }
- struct fenwick {
- vector <int> s;
- fenwick(int n) {
- s.resize(n + 1, 0);
- }
- int get(int r) {
- int sum = 0;
- for (int i = r; i >= 0; i = f(i) - 1) {
- sum += s[i];
- }
- return sum;
- }
- void up(int r, int q) {
- for (int i = r; i < s.size(); i = g(i)) {
- s[i] += q;
- }
- }
- };
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout.setf(ios::fixed);
- cout.precision(10);
- int n;
- cin >> n;
- vector <long long> fact(n + 1, 1);
- for (long long i = 1; i <= n; ++i) {
- fact[i] = (fact[i - 1] * i) % mod;
- }
- fenwick t(n);
- long long ans = 0;
- for (int i = 0; i < n; ++i) {
- int a;
- cin >> a;
- ans += fact[n - i - 1] * (a - t.get(a - 1) - 1);
- ans %= mod;
- t.up(a, 1);
- }
- cout << ans + 1 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement