#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;
}
