#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << (x) << endl
using namespace std;

const int N = 200005;
const long long mod = 1000000007ll;

template <class T>
void read (T &x) {
	int sgn = 1;
	char ch;
	x = 0;
	for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ;
	if (ch == '-') ch = getchar(), sgn = -1;
	for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	x *= sgn;
}

template <class T>
void write (T x) {
	if (x < 0) putchar('-'), write(-x);
	else if (x < 10) putchar(x + '0');
	else write(x / 10), putchar(x % 10 + '0');
}

struct node {
	int id, lim;
	bool operator < (node rhs) const {
		return lim < rhs.lim;
	}
} vec[N];
int n, sgt_cnt[N << 2];
long long inv[N], sgt[N << 2], sgt_tag[N << 2], res = 1ll, ans = 0ll;
bool flag = true;

void build (int l, int r, int now) {
	int mid = l + r >> 1;
	sgt_cnt[now] = 0, sgt[now] = 0ll, sgt_tag[now] = 1ll;
	if (l < r) {
		build(l, mid, now << 1);
		build(mid + 1, r, now << 1 | 1);
	}
}
void pushup (int now) {
	sgt_cnt[now] = sgt_cnt[now << 1] + sgt_cnt[now << 1 | 1];
	sgt[now] = (sgt[now << 1] + sgt[now << 1 | 1]) % mod;
}
void cover (int now, long long t) {
	sgt[now] = sgt[now] * t % mod;
	sgt_tag[now] = sgt_tag[now] * t % mod;
}
void pushdown (int now) {
	cover(now << 1, sgt_tag[now]);
	cover(now << 1 | 1, sgt_tag[now]);
	sgt_tag[now] = 1ll;
}

void change (int pos, int l, int r, int now, long long val) {
	int mid = l + r >> 1;
	if (l == r) sgt_cnt[now] = 1, sgt[now] = val;
	else {
		pushdown(now);
		if (pos <= mid) change(pos, l, mid, now << 1, val);
		else if (pos > mid) change(pos, mid + 1, r, now << 1 | 1, val);
		pushup(now);
	}
}

int query1 (int left, int right, int l, int r, int now) {
	int mid = l + r >> 1;
	if (l == left && r == right) return sgt_cnt[now];
	else {
		pushdown(now);
		if (right <= mid) return query1(left, right, l, mid, now << 1);
		else if (left > mid) return query1(left, right, mid + 1, r, now << 1 | 1);
		else return query1(left, mid, l, mid, now << 1) + query1(mid + 1, right, mid + 1, r, now << 1 | 1);
	}
}

long long query2 (int left, int right, int l, int r, int now) {
	int mid = l + r >> 1;
	if (l == left && r == right) return sgt[now];
	else {
		pushdown(now);
		if (right <= mid) return query2(left, right, l, mid, now << 1);
		else if (left > mid) return query2(left, right, mid + 1, r, now << 1 | 1);
		else return (query2(left, mid, l, mid, now << 1) + query2(mid + 1, right, mid + 1, r, now << 1 | 1)) % mod;
	}
}

void init () {
	inv[1] = 1ll;
	for (int i = 2; i <= max(n, 2); i++) inv[i] = (mod / i) * (mod - inv[mod % i]) % mod;
}

void solve () {
	build(1, n, 1);
	for (int i = 0; i < n; i++) {
		int cnt = query1(vec[i].id, n, 1, n, 1);
		long long l = query2(1, vec[i].id, 1, n, 1), r = query2(vec[i].id, n, 1, n, 1);
		l = l * res % mod * inv[vec[i].lim - i] % mod;
		r = r * res % mod * inv[vec[i].lim - i] % mod;
		ans = (ans + res * cnt + l - r + mod) % mod;

		sgt_tag[1] = sgt_tag[1] * (vec[i].lim - i - 1) % mod * inv[vec[i].lim - i] % mod;
		sgt[1] = sgt[1] * (vec[i].lim - i - 1) % mod * inv[vec[i].lim - i] % mod;
		change(vec[i].id, 1, n, 1, inv[2] * (vec[i].lim - i - 1) % mod);
	}
}

int main () {
	read(n);
	for (int i = 0; i < n; i++) vec[i].id = i + 1, read(vec[i].lim);

	sort(vec, vec + n);
	for (int i = 0; i < n; i++) {
		if (vec[i].lim <= i) flag = false;
		res = res * (vec[i].lim - i) % mod;
	}

	if (flag) init(), solve();
	write(ans), putchar('\n');
	return 0;
}

Measured memory_bytes: 18944