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


Measured memory_bytes: 9600