#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

const int MOD = (int) 1e9 + 7;
const int inv2 = MOD - MOD / 2;

class SegmentTree {
public:
    SegmentTree(int n):
        ans(4 * n + 5, 0),
        lazyMul(4 * n + 5, 1),
        n(n) {}
    void add(int pos, int val) {
        add(0, 0, n - 1, pos, val);
    }
    void mul(int left, int right, int val) {
        mul(0, 0, n - 1, left, right, val);
    }
    int query(int left, int right) {
        if (left > right) {
            return 0;
        }
        return query(0, 0, n - 1, left, right);
    }
private:
    vector<int> ans, lazyMul;
    int n;

    void split(int node, int left, int right) {
        if (lazyMul[node] != 1) {
            ans[node] = ans[node] * (int64_t) lazyMul[node] % MOD;
            if (left < right) {
                lazyMul[2 * node + 1] = lazyMul[2 * node + 1] * 
                    (int64_t) lazyMul[node] % MOD;
                lazyMul[2 * node + 2] = lazyMul[2 * node + 2] * 
                    (int64_t) lazyMul[node] % MOD;
            }
            lazyMul[node] = 1;
        }
    }

    void add(int node, int left, int right, int pos, int val) {
        split(node, left, right);
        if (left == right) {
            ans[node] = (ans[node] + val) % MOD;
        } else {
            int mid = (left + right) / 2;
            if (pos <= mid) {
                add(2 * node + 1, left, mid, pos, val);
            } else {
                add(2 * node + 2, mid + 1, right, pos, val);
            }
            split(2 * node + 1, left, mid);
            split(2 * node + 2, mid + 1, right);
            ans[node] = (ans[2 * node + 1] + ans[2 * node + 2]) % MOD;
        }
    }

    void mul(int node, int left, int right, int lq, int rq, int val) {
        split(node, left, right);
        if (lq <= left && right <= rq) {
            lazyMul[node] = val;
        } else {
            int mid = (left + right) / 2;
            if (lq <= mid) {
                mul(2 * node + 1, left, mid, lq, rq, val);
            }
            if (rq > mid) {
                mul(2 * node + 2, mid + 1, right, lq, rq, val);
            }
            split(2 * node + 1, left, mid);
            split(2 * node + 2, mid + 1, right);
            ans[node] = (ans[2 * node + 1] + ans[2 * node + 2]) % MOD;
        }
    }

    int query(int node, int left, int right, int lq, int rq) {
        split(node, left, right);
        if (lq <= left && right <= rq) {
            return ans[node];
        } else {
            int mid = (left + right) / 2;
            int ret = 0;
            if (lq <= mid) {
                ret = query(2 * node + 1, left, mid, lq, rq);
            }
            if (rq > mid) {
                ret = (ret + query(2 * node + 2, mid + 1, right, lq, rq)) % MOD;
            }
            return ret;
        }
    }
};

class FenwickTree {
public:
    FenwickTree(int n):
        tree(n + 1, 0) {}
    int query(int pos) const {
        int ret = 0;
        for (++pos; pos > 0; pos -= pos & -pos) {
            ret += tree[pos];
        }
        return ret;
    }
    void update(int pos, int val) {
        for (++pos; pos < SZ(tree); pos += pos & -pos) {
            tree[pos] += val;
        }
    }
private:
    vector<int> tree;
};

int32_t main() {
    #ifdef LOCAL_RUN
    freopen("task.in", "r", stdin);
    freopen("task.out", "w", stdout);
    //freopen("task.err", "w", stderr);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first;
        a[i].second = i;
        a[i].first = min(a[i].first, n);
    }
    sort(a.begin(), a.end());
    vector<int> part(n + 1, 1);
    for (int i = n - 1; i >= 0; --i) {
        part[i] = part[i + 1] * (int64_t) (a[i].first - i) % MOD;
    }
    SegmentTree t(n);
    FenwickTree cnt(n);
    int prod = 1;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int sum1 = t.query(0, a[i].second - 1);
        int sum2 = t.query(a[i].second + 1, n - 1);
        int cnt1 = cnt.query(a[i].second);
        int cnt2 = i - cnt1;
        ans = (ans + cnt2 * (int64_t) part[0]) % MOD;
        ans = (ans + sum1 * (int64_t) part[i + 1] % MOD * inv2) % MOD;
        ans = (ans - sum2 * (int64_t) part[i + 1] % MOD * inv2) % MOD;
        t.mul(0, n - 1, a[i].first - i - 1);
        int v = prod * (int64_t) (a[i].first - i) % MOD * (a[i].first - i - 1) % MOD;
        t.add(a[i].second, v);
        cnt.update(a[i].second, 1);
        prod = prod * (int64_t) (a[i].first - i) % MOD;
    }
    
    ans %= MOD;
    if (ans < 0) {
        ans += MOD;
    }
    cout << ans << '\n';
}

### Prompt

In Cpp, your task is to solve the following problem:

Snuke has an integer sequence A whose length is N. He likes permutations of (1, 2, ..., N), P, that satisfy the following condition:

* P_i \leq A_i for all i ( 1 \leq i \leq N ).



Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

* 1 \leq N \leq 2 \times 10^5
* 1 \leq A_i \leq N ( 1 \leq i \leq N )
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 A_2 ... A_N


Output

Print the sum of the inversion numbers over all permutations that satisfy the condition.

Examples

Input

3
2 3 3


Output

4


Input

6
4 2 5 1 6 3


Output

7


Input

5
4 4 4 4 4


Output

0


Input

30
22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23


Output

848414012

### Response

```cpp
#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

const int MOD = (int) 1e9 + 7;
const int inv2 = MOD - MOD / 2;

class SegmentTree {
public:
    SegmentTree(int n):
        ans(4 * n + 5, 0),
        lazyMul(4 * n + 5, 1),
        n(n) {}
    void add(int pos, int val) {
        add(0, 0, n - 1, pos, val);
    }
    void mul(int left, int right, int val) {
        mul(0, 0, n - 1, left, right, val);
    }
    int query(int left, int right) {
        if (left > right) {
            return 0;
        }
        return query(0, 0, n - 1, left, right);
    }
private:
    vector<int> ans, lazyMul;
    int n;

    void split(int node, int left, int right) {
        if (lazyMul[node] != 1) {
            ans[node] = ans[node] * (int64_t) lazyMul[node] % MOD;
            if (left < right) {
                lazyMul[2 * node + 1] = lazyMul[2 * node + 1] * 
                    (int64_t) lazyMul[node] % MOD;
                lazyMul[2 * node + 2] = lazyMul[2 * node + 2] * 
                    (int64_t) lazyMul[node] % MOD;
            }
            lazyMul[node] = 1;
        }
    }

    void add(int node, int left, int right, int pos, int val) {
        split(node, left, right);
        if (left == right) {
            ans[node] = (ans[node] + val) % MOD;
        } else {
            int mid = (left + right) / 2;
            if (pos <= mid) {
                add(2 * node + 1, left, mid, pos, val);
            } else {
                add(2 * node + 2, mid + 1, right, pos, val);
            }
            split(2 * node + 1, left, mid);
            split(2 * node + 2, mid + 1, right);
            ans[node] = (ans[2 * node + 1] + ans[2 * node + 2]) % MOD;
        }
    }

    void mul(int node, int left, int right, int lq, int rq, int val) {
        split(node, left, right);
        if (lq <= left && right <= rq) {
            lazyMul[node] = val;
        } else {
            int mid = (left + right) / 2;
            if (lq <= mid) {
                mul(2 * node + 1, left, mid, lq, rq, val);
            }
            if (rq > mid) {
                mul(2 * node + 2, mid + 1, right, lq, rq, val);
            }
            split(2 * node + 1, left, mid);
            split(2 * node + 2, mid + 1, right);
            ans[node] = (ans[2 * node + 1] + ans[2 * node + 2]) % MOD;
        }
    }

    int query(int node, int left, int right, int lq, int rq) {
        split(node, left, right);
        if (lq <= left && right <= rq) {
            return ans[node];
        } else {
            int mid = (left + right) / 2;
            int ret = 0;
            if (lq <= mid) {
                ret = query(2 * node + 1, left, mid, lq, rq);
            }
            if (rq > mid) {
                ret = (ret + query(2 * node + 2, mid + 1, right, lq, rq)) % MOD;
            }
            return ret;
        }
    }
};

class FenwickTree {
public:
    FenwickTree(int n):
        tree(n + 1, 0) {}
    int query(int pos) const {
        int ret = 0;
        for (++pos; pos > 0; pos -= pos & -pos) {
            ret += tree[pos];
        }
        return ret;
    }
    void update(int pos, int val) {
        for (++pos; pos < SZ(tree); pos += pos & -pos) {
            tree[pos] += val;
        }
    }
private:
    vector<int> tree;
};

int32_t main() {
    #ifdef LOCAL_RUN
    freopen("task.in", "r", stdin);
    freopen("task.out", "w", stdout);
    //freopen("task.err", "w", stderr);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<pair<int, int>> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first;
        a[i].second = i;
        a[i].first = min(a[i].first, n);
    }
    sort(a.begin(), a.end());
    vector<int> part(n + 1, 1);
    for (int i = n - 1; i >= 0; --i) {
        part[i] = part[i + 1] * (int64_t) (a[i].first - i) % MOD;
    }
    SegmentTree t(n);
    FenwickTree cnt(n);
    int prod = 1;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int sum1 = t.query(0, a[i].second - 1);
        int sum2 = t.query(a[i].second + 1, n - 1);
        int cnt1 = cnt.query(a[i].second);
        int cnt2 = i - cnt1;
        ans = (ans + cnt2 * (int64_t) part[0]) % MOD;
        ans = (ans + sum1 * (int64_t) part[i + 1] % MOD * inv2) % MOD;
        ans = (ans - sum2 * (int64_t) part[i + 1] % MOD * inv2) % MOD;
        t.mul(0, n - 1, a[i].first - i - 1);
        int v = prod * (int64_t) (a[i].first - i) % MOD * (a[i].first - i - 1) % MOD;
        t.add(a[i].second, v);
        cnt.update(a[i].second, 1);
        prod = prod * (int64_t) (a[i].first - i) % MOD;
    }
    
    ans %= MOD;
    if (ans < 0) {
        ans += MOD;
    }
    cout << ans << '\n';
}

```