#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>

using namespace std;

typedef long long ll;

mt19937 rnd(228);

const int M = 1e9 + 7;

inline int add(int a, int b)
{
    a += b;
    if (a >= M) a -= M;
    if (a < 0) a += M;
    return a;
}

inline int mul(int a, int b)
{
    return (a * (ll) b) % M;
}

const int N = 2e5 + 7;

int t[4 * N];
int upd_add[4 * N];
int upd_mul[4 * N];
int cnt[4 * N];

void upd(int v, int k, int b)
{
    t[v] = add(mul(t[v], k), mul(b, cnt[v]));
    upd_add[v] = add(mul(upd_add[v], k), b);
    upd_mul[v] = mul(upd_mul[v], k);
}

void push(int v)
{
    upd(v * 2 + 1, upd_mul[v], upd_add[v]);
    upd(v * 2 + 2, upd_mul[v], upd_add[v]);
    upd_mul[v] = 1, upd_add[v] = 0;
}

void upd(int v, int l, int r, int tl, int tr, int k, int b)
{
    if (tl >= r || tr <= l)
    {
        return;
    }
    if (tl >= l && tr <= r)
    {
        upd(v, k, b);
    }
    else
    {
        int tm = (tl + tr) / 2;
        push(v);
        upd(v * 2 + 1, l, r, tl, tm, k, b);
        upd(v * 2 + 2, l, r, tm, tr, k, b);
        t[v] = add(t[v * 2 + 1], t[v * 2 + 2]);
    }
}

int get(int v, int l, int r, int tl, int tr)
{
    if (tl >= r || tr <= l)
    {
        return 0;
    }
    if (tl >= l && tr <= r)
    {
        return t[v];
    }
    else
    {
        int tm = (tl + tr) / 2;
        push(v);
        return add(get(v * 2 + 1, l, r, tl, tm), get(v * 2 + 2, l, r, tm, tr));
    }
}

void build(int v, int l, int r)
{
    upd_mul[v] = 1;
    upd_add[v] = t[v] = 0;
    cnt[v] = r - l;
    if (r - l != 1)
    {
        int m = (l + r) / 2;
        build(v * 2 + 1, l, m);
        build(v * 2 + 2, m, r);
    }
}

int fenw[N];

void inc(int x, int y)
{
    for (; x < N; x = (x | (x + 1)))
    {
        fenw[x] += y;
    }
}

int get(int x)
{
    int ans = 0;
    for (; x >= 0; x = (x & (x + 1)) - 1)
    {
        ans += fenw[x];
    }
    return ans;
}

int suf(int x)
{
    return get(N - 1) - get(x);
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    build(0, 0, n + 1);
    vector <int> a(n);
    vector <int> cnt(n + 1);
    vector <vector <int> > on(n + 1);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        on[n - a[i] + 1].push_back(i + 1);
        cnt[a[i]]++;
    }
    for (int i = n - 1; i >= 0; i--)
    {
        cnt[i] += cnt[i + 1];
    }
    for (int i = n; i >= 1; i--)
    {
        if (cnt[i] <= n - i)
        {
            cout << 0 << '\n';
            return 0;
        }
    }
    reverse(cnt.begin() + 1, cnt.end());
    vector <int> dp(n + 1);
    vector <int> ct(n + 1);
    dp[0] = 1;
    cnt[0] = 0;
    int rev_2 = (M + 1) / 2;
    int sum = 0;
    for (int i = 1; i <= n; i++) inc(i, 1);
    for (int i = 1; i <= n; i++)
    {
        for (int go : on[i])
        {
            sum = add(sum, -get(0, 0, go, 0, n + 1));
            inc(go, -1);
        }
        for (int go : on[i])
        {
            upd(0, go, go + 1, 0, n + 1, 1, dp[i - 1]);
            sum = add(sum, mul(dp[i - 1], suf(go)));
        }
        int x = cnt[i] - (i - 1);
        int x_sqr = mul(mul(x, (x - 1)), rev_2);
        ct[i] = add(add(mul(x_sqr, dp[i - 1]), mul(ct[i - 1], x)), sum);
        dp[i] = mul(dp[i - 1], x);
        sum = mul(sum, (x - 1));
        upd(0, 0, n + 1, 0, n + 1, x - 1, 0);
    }
    cout << ct[n] << '\n';
}


Measured memory_bytes: 26624