# include <bits/stdc++.h>
/*
// ordered_set 
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/

using namespace std;
 
typedef long long                                        ll;
typedef long double                                      ld;
typedef pair <int, int>                                  pii;
typedef pair <pii, int>                                  ppi;
typedef pair <int, pii>                                  pip;
typedef pair <pii, pii>                                  ppp;
typedef pair <ll , ll >                                  pll;
 
# define A                                               first
# define B                                               second
# define endl                                            '\n'
# define sep                                             ' '
# define all(x)                                          x.begin(), x.end()
# define kill(x)                                         return cout << x << endl, 0
# define SZ(x)                                           int(x.size())
# define Sort(x)                                         sort(all(x))
# define InTheNameOfGod                                  ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
 
const ll md = 1e9 + 7;//998244353;
const int xn = 2e5 + 10;
const int xm = -20 + 10;
const int SQ = 320;
const int sq = SQ + 10;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
 
ll power(ll a, ll b) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2) % md : power(a * a % md, b / 2) % md));}
ll zarb(ll a, ll b){return (a * b + 100 * md) % md;}
ll jaam(ll a, ll b){return (a + b + 100 * md) % md;}
string abc = "abcdefghijklmnopqrstuvwxyz";

ll n, Tot = 1, b[xn], ans, res, inv[2][xn], s, INV = power(2, md - 2), c[xn], C[xn], ind, part[4][xn], last[xn];
pll num[xn], Order[xn], DS, seg[xn << 2], lazy[xn << 2];

pll merge(pll a, pll b){
	pll c;
	c.A = (a.A + b.A) % md;
	c.B = (a.B + b.B) % md;
	return c;
}
void Shift(int id, int L, int R){
	if (!lazy[id].A && !lazy[id].B) return;
	seg[id].A = (seg[id].A + lazy[id].A) % md;
	seg[id].B = (seg[id].B + lazy[id].B) % md;
	if (R - L > 1){
		lazy[id << 1].A = (lazy[id << 1].A + lazy[id].A) % md;
		lazy[id << 1 | 1].A = (lazy[id << 1 | 1].A + lazy[id].A) % md;
		lazy[id << 1].B = (lazy[id << 1].B + lazy[id].B) % md;
		lazy[id << 1 | 1].B = (lazy[id << 1 | 1].B + lazy[id].B) % md;
	}
	lazy[id].A = lazy[id].B = 0;
}
void update(int id, int L, int R, int Ql, int Qr, ll val){
	Shift(id, L, R);
	if (Qr <= L || R <= Ql) return;
	if (Ql <= L && R <= Qr){
		++ lazy[id].A;
		lazy[id].B = (lazy[id].B + val) % md;
		Shift(id, L, R);
		return;
	}
	int Mid = (L + R) / 2;
	update(id << 1, L, Mid, Ql, Qr, val);
	update(id << 1 | 1, Mid, R, Ql, Qr, val);
	seg[id].A = (seg[id << 1].A + seg[id << 1 | 1].A) % md;
	seg[id].B = (seg[id << 1].B + seg[id << 1 | 1].B) % md;
}
pll get(int id, int L, int R, int Ql, int Qr){
	Shift(id, L, R);
	if (Qr <= L || R <= Ql) return {0, 0};
	if (Ql <= L && R <= Qr) return seg[id];
	int Mid = (L + R) / 2;
	return merge(get(id << 1, L, Mid, Ql, Qr), get(id << 1 | 1, Mid, R, Ql, Qr));
}

int main(){
	InTheNameOfGod;
	
	cin >> n;
	for (ll i = 0; i < n; ++ i) cin >> num[i].A, num[i].B = i;
	sort(num, num + n);
	for (ll i = 0; i < n; ++ i){
		if (num[i].A > n) kill(0);
		b[i] = (num[i].A - i);
		if (b[i] <= 0) kill(0);
		Tot = Tot * b[i] % md;
		inv[0][i] = power(b[i], md - 2);
		inv[1][i] = power((b[i] - 1), md - 2);
		if (b[i] - 1 == 0) inv[1][i] = 1;
		C[i] = ((b[i] * (b[i] - 1)) % md * INV) % md;
		Order[i] = {num[i].B, i};
	}
	part[0][0] = max(1LL, b[0] - 1);
	part[1][0] = inv[1][0];
	part[3][0] = b[0];
	part[2][n] = 1;
	for (ll i = 1; i < n; ++ i){
		part[0][i] = (part[0][i - 1] * (max(1LL, b[i] - 1))) % md;
		part[1][i] = (part[1][i - 1] * inv[1][i]) % md;
		part[3][i] = (part[3][i - 1] * b[i]) % md;
	}
	for (ll i = n - 1; i >= 0; -- i)
		part[2][i] = (part[2][i + 1] * b[i]) % md;
	last[n] = n;
	for (ll i = n - 1; i >= 0; -- i){
		if (b[i] == 1) last[i] = i;
		else last[i] = last[i + 1];
	}
	for (ll i = 1; i < n; ++ i){
		c[i] = (c[i - 1] * (b[i - 1] - 1)) % md;
		c[i] = (c[i] * inv[0][i]) % md;
		c[i] = (c[i] + ((inv[0][i] * inv[0][i - 1] % md) * (Tot * C[i - 1] % md)) % md) % md;
		ans = (ans + c[i]) % md;
	}
	sort(Order, Order + n);
	reverse(Order, Order + n);
	for (ll i = 0; i < n; ++ i){
		ind = Order[i].B;
		res = (part[0][ind - 1] * part[2][ind + 1]) % md;
		DS = get(1, 0, xn, ind, ind + 1);
		res = (res * DS.B) % md;
		ans = (ans - res - res + md + md) % md;
		ans = (ans + (Tot * DS.A) % md) % md;
		if (ind == 0) s = (part[1][ind]);
		else s = (part[1][ind] * part[3][ind - 1]) % md;
		s = (s * C[ind]) % md;
		update(1, 0, xn, ind + 1, last[ind] + 1, s);
		update(1, 0, xn, last[ind] + 1, xn, 0);
	}
	cout << ans << endl;
	
	return 0;
}


Measured memory_bytes: 43008