#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using ll = int64_t;
using ld = long double;
using ull = uint64_t;
using namespace std;
using namespace __gnu_pbds;

const ll MOD = 1e9 + 7;
const ll REV2 = (MOD + 1) / 2;

ll pw(ll a, ll b) {
    if (!b) {
        return 1;
    }

    ll v = pw(a, b / 2);
    v = (v * v) % MOD;

    if (b & 1) {
        v = (v * a) % MOD;
    }

    return v;
}

ll rev(ll a) {
    return pw(a, MOD - 2);
}

const int MAXN = 200228;
pair<int, int> a[MAXN];
int n;

struct Fen {
    int vers = 1;
    ll f[MAXN];
    int v[MAXN];

    ll& at(int x) {
        if (v[x] < vers) {
            v[x] = vers;
            f[x] = 0;
        }

        return f[x];
    }

    void upd(int x, ll y) {
        for (int i = x; i <= n; i |= (i + 1)) {
            at(i) += y;
        }
    }

    ll gt(int x) {
        ll ans = 0;
        for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
            ans += at(i);
        }

        return ans;
    }
};

ll v[MAXN];

ll g(ll x) {
    x %= MOD;
    if (x < 0) {
        x += MOD;
    }

    return x;
}

Fen fc;
Fen sb;

int main() {
#ifdef PAUNSVOKNO
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20);  
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i].first;
        a[i].second = i;
    }

    sort(a, a + n);
   

    ll tot = 1;

    for (int i = 0; i < n; ++i) {
        v[i] = a[i].first - i;
        tot = (tot * v[i]) % MOD;
    }

    if (!tot) {
        cout << "0\n";
        return 0;
    }

    ll inveach = 0;
    ll sub = 0;
    ll mdf = 1;

    for (int i = n - 1; i >= 0; --i) {
        ll rv = rev(v[i]);

        inveach += fc.gt(a[i].second);
        fc.upd(a[i].second, 1);

        ll cur = (v[i] - 1) * rv % MOD;
        if (cur == 0) {
            sb.vers++;
            mdf = 1;
        } else {
            mdf = (mdf * cur) % MOD;
        }

        ll sl = sb.gt(a[i].second);
        ll sr = sb.gt(n) - sl;

        ll cs = g(sl - sr);
        cs = g(cs * mdf);
        cs = g(cs * v[i]);
        sub += cs;

        sb.upd(a[i].second, rv * rev(mdf) % MOD);
    }

    ll each = g(inveach - sub * REV2);

    cout << (tot * each) % MOD;
}