#include <bits/stdc++.h>

#define reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
    const int MOD = 1000000007;
    const int INV2 = (MOD + 1) / 2;
    inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
    inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
    inline int mul(int x, int y) { return 1LL * x * y % MOD; }
    inline int qpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = mul(x, x))
            if (y & 1) ans = mul(ans, x);
        return ans;
    }
};  // namespace modular
using namespace modular;

namespace Base {
    template <typename Tp>
    inline Tp input() {
        Tp x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return 0;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x *= y;
    }
    template <typename Tp>
    inline void read(Tp &x) {
        x = input<Tp>();
    }
    template <typename Tp>
    inline void chmax(Tp &x, Tp y) {
        x < y ? x = y : 0;
    }
    template <typename Tp>
    inline void chmin(Tp &x, Tp y) {
        x > y ? x = y : 0;
    }
};  // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 200007

int N;
int A[MAX_N];

struct BIT {
    int v[MAX_N], c[MAX_N];
    inline int lowbit(int x) { return x & -x; }
    void ins(int x, int val) {
        while (x <= N) {
            inc(v[x], val);
            c[x]++;
            x += lowbit(x);
        }
    }
    int query(int x) {
        if (x <= 0) return 0;
        int ans = 0;
        while (x > 0) inc(ans, v[x]), x ^= lowbit(x);
        return ans;
    }
    int getn(int x) {
        int ans = 0;
        while (x > 0) inc(ans, c[x]), x ^= lowbit(x);
        return ans;
    }
    int qr(int l, int r) { return add(query(r), MOD - query(l - 1)); }
} tr;

int P, p[MAX_N], ip[MAX_N], c0[MAX_N];
int bg[MAX_N], ed[MAX_N], cnt[MAX_N];

void init() {
    IRep(i, N) cnt[i] += cnt[i + 1];
    p[0] = 1;
    P = 1;
    Rep(i, N) {
        cnt[i] -= N - i;
        if (cnt[i] <= 0) {
            puts("0");
            exit(0);
        }
        P = mul(P, cnt[i]);
    }
    Rep(i, N) {
        c0[i] = c0[i - 1], p[i] = p[i - 1];
        int tmp = mul(cnt[i] - 1, qpow(cnt[i], MOD - 2));
        if (!tmp)
            c0[i]++, bg[c0[i]] = i, ed[c0[i] - 1] = i - 1;
        else
            p[i] = mul(p[i], tmp);
        ip[i] = qpow(p[i], MOD - 2);
    }
    ed[c0[N]] = N;
}

void solve() {
    int res = 0;
    Rep(i, N) {
        int x = A[i], tmp = mul(mul(P, INV2), p[x]);
        inc(res, mul(tmp, tr.qr(bg[c0[x]], x)));
        tr.ins(x, ip[x]);
    }
    std::memset(tr.v, 0, sizeof(tr.v));
    std::memset(tr.c, 0, sizeof(tr.c));
    Rep(i, N) {
        int x = A[i],
            tmp = mul(mul(P, INV2), mul(tr.qr(x + 1, ed[c0[x]]), ip[x]));
        inc(res, add(mul(tr.getn(N) - tr.getn(A[i]), P), MOD - tmp));
        tr.ins(x, p[x]);
    }
    printf("%d\n", res);
}

int main() {
#ifdef LOCAL
    FIN("in");
#endif
    read(N);
    Rep(i, N) read(A[i]), cnt[A[i]]++;
    init();
    solve();
    return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

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 reg register
#define pr std::pair<int, int>
#define fi first
#define se second
#define FIN(s) freopen(s, "r", stdin)
#define FOUT(s) freopen(s, "w", stdout)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define lep(i, l, r) for (int i = l; i < r; ++i)
#define irep(i, r, l) for (int i = r; i >= l; --i)
#define ilep(i, r, l) for (int i = r; i > l; --i)
#define Rep(i, n) rep(i, 1, n)
#define Lep(i, n) lep(i, 1, n)
#define IRep(i, n) irep(i, n, 1)
#define ILep(i, n) ilep(i, n, 1)
typedef long long ll;
typedef long double ld;

namespace modular {
    const int MOD = 1000000007;
    const int INV2 = (MOD + 1) / 2;
    inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }
    inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }
    inline int mul(int x, int y) { return 1LL * x * y % MOD; }
    inline int qpow(int x, int y) {
        int ans = 1;
        for (; y; y >>= 1, x = mul(x, x))
            if (y & 1) ans = mul(ans, x);
        return ans;
    }
};  // namespace modular
using namespace modular;

namespace Base {
    template <typename Tp>
    inline Tp input() {
        Tp x = 0, y = 1;
        char c = getchar();
        while ((c < '0' || '9' < c) && c != EOF) {
            if (c == '-') y = -1;
            c = getchar();
        }
        if (c == EOF) return 0;
        while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x *= y;
    }
    template <typename Tp>
    inline void read(Tp &x) {
        x = input<Tp>();
    }
    template <typename Tp>
    inline void chmax(Tp &x, Tp y) {
        x < y ? x = y : 0;
    }
    template <typename Tp>
    inline void chmin(Tp &x, Tp y) {
        x > y ? x = y : 0;
    }
};  // namespace Base
using namespace Base;
/*----------------------------------------------------------------------------*/

#define MAX_N 200007

int N;
int A[MAX_N];

struct BIT {
    int v[MAX_N], c[MAX_N];
    inline int lowbit(int x) { return x & -x; }
    void ins(int x, int val) {
        while (x <= N) {
            inc(v[x], val);
            c[x]++;
            x += lowbit(x);
        }
    }
    int query(int x) {
        if (x <= 0) return 0;
        int ans = 0;
        while (x > 0) inc(ans, v[x]), x ^= lowbit(x);
        return ans;
    }
    int getn(int x) {
        int ans = 0;
        while (x > 0) inc(ans, c[x]), x ^= lowbit(x);
        return ans;
    }
    int qr(int l, int r) { return add(query(r), MOD - query(l - 1)); }
} tr;

int P, p[MAX_N], ip[MAX_N], c0[MAX_N];
int bg[MAX_N], ed[MAX_N], cnt[MAX_N];

void init() {
    IRep(i, N) cnt[i] += cnt[i + 1];
    p[0] = 1;
    P = 1;
    Rep(i, N) {
        cnt[i] -= N - i;
        if (cnt[i] <= 0) {
            puts("0");
            exit(0);
        }
        P = mul(P, cnt[i]);
    }
    Rep(i, N) {
        c0[i] = c0[i - 1], p[i] = p[i - 1];
        int tmp = mul(cnt[i] - 1, qpow(cnt[i], MOD - 2));
        if (!tmp)
            c0[i]++, bg[c0[i]] = i, ed[c0[i] - 1] = i - 1;
        else
            p[i] = mul(p[i], tmp);
        ip[i] = qpow(p[i], MOD - 2);
    }
    ed[c0[N]] = N;
}

void solve() {
    int res = 0;
    Rep(i, N) {
        int x = A[i], tmp = mul(mul(P, INV2), p[x]);
        inc(res, mul(tmp, tr.qr(bg[c0[x]], x)));
        tr.ins(x, ip[x]);
    }
    std::memset(tr.v, 0, sizeof(tr.v));
    std::memset(tr.c, 0, sizeof(tr.c));
    Rep(i, N) {
        int x = A[i],
            tmp = mul(mul(P, INV2), mul(tr.qr(x + 1, ed[c0[x]]), ip[x]));
        inc(res, add(mul(tr.getn(N) - tr.getn(A[i]), P), MOD - tmp));
        tr.ins(x, p[x]);
    }
    printf("%d\n", res);
}

int main() {
#ifdef LOCAL
    FIN("in");
#endif
    read(N);
    Rep(i, N) read(A[i]), cnt[A[i]]++;
    init();
    solve();
    return 0;
}

```