/*
ЗАПУСКАЕМ 
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░ 
 */
#pragma GCC optimize "-oFast"
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
    
     
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } 
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define rank rank228
#define y1 y1228                                                         
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
const string FILENAME = "input";
const int Mod = 1000000007;
const int MAXN = 200228;


int sume(int a, int b) {
    return (a + b) % Mod;
}


int mul(int a, int b) {
    return (1LL * a * b) % Mod;
}


int powm(int a, int b) {
    if (b == 0) {
        return 1; 
    } else {
        if (b & 1) {
            return mul(powm(a, b - 1), a);
        } else {
            int g = powm(a, b >> 1);
            return mul(g, g);
        }
    }
}


int n;
int a[MAXN];
int p, r = 1;
vector<int> x[MAXN];
pair<int, int> u = {1, 0};
pair<int, pair<int, int> > f[MAXN];


inline void operator += (pair<int, pair<int, int> > &a, pair<int, pair<int, int> > &b) {
    if (a.second.second<u.second) {
       a.second.first = 0;
       a.second.second = u.second;
    }
    if (b.second.second < u.second) {
       b.second.first = 0;
       b.second.second = u.second;
    }
    a.first += b.first;
    a.second.first = sume(sume(a.second.first, 0), sume(b.second.first, 0));
}


inline pair<int, pair<int, int> > sum(int i) {
    pair<int, pair<int, int> > k = {};
    for(; i; i ^= i&-i) {
        k += f[i];
    }
    return k;
}


inline void add(int i, pair<int, pair<int, int> > k) {
    for(; i <= n; i += i & -i) {
        f[i] += k;
    }
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    //read(FILENAME);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        x[a[i]].pb(i);
    }
    for (int i = n, k = 1; i > 0; i--) {
        k--;
       // cout << i << endl;
        int j = sz(x[i]);
        p = sume(p, mul((1LL * j * (j - 1) / 2 + 1LL * j * k) % Mod, 500000004));
        k += j;
        //cout << i << endl;
        for (auto l: x[i]) {
            pair<int, pair<int, int> > w = sum(l - 1);
            p = sume(sume(p, w.first), Mod - mul(w.second.first, u.first));
        }
                //cout << i << endl;
        for (auto l: x[i]) {
            add(l, make_pair(1, make_pair(powm(u.first, Mod - 2), u.second)));
        }
        //cout << i << endl;
        if (k !=  1) {
            u.first = mul(u.first, sume(1, Mod - powm(k, Mod - 2)));
        } else {
            u.second++; 
        }
        r = mul(r, k);
    }
    p = mul(p, r);
    p = sume(p, Mod);
    cout << p << '\n';
    return 0;
}


Measured memory_bytes: 14336