#include <bits/stdc++.h>

const int MAXN = 200010;
const int mod = 1000000007;
const int inv2 = mod + 1 >> 1;
typedef long long LL;
void reduce(int & x) { x += x >> 31 & mod; }
int mul(int a, int b) { return (LL) a * b % mod; }
int pow(int a, int b, int res = 1) {
	for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a);
	return res;
}
int A[MAXN], rk[MAXN], n, B[MAXN], pb[MAXN], sb[MAXN];
struct seg {
	int tree[MAXN << 2], tagmul[MAXN << 2];
	seg() { std::fill(tagmul, tagmul + (MAXN << 2), 1); }
	void mkmul(int u, int v) {
		tree[u] = mul(tree[u], v);
		tagmul[u] = mul(tagmul[u], v);
	}
	void pushdown(int u) {
		if (tagmul[u] != 1) {
			mkmul(u << 1, tagmul[u]);
			mkmul(u << 1 | 1, tagmul[u]);
			tagmul[u] = 1;
		}
	}
	void add(int u, int l, int r, int tar, int v) {
		if (l == r) return (void) (tree[u] += v);
		int mid = l + r >> 1;
		pushdown(u);
		if (tar <= mid) add(u << 1, l, mid, tar, v);
		else add(u << 1 | 1, mid + 1, r, tar, v);
		reduce(tree[u] = tree[u << 1] + tree[u << 1 | 1] - mod);
	}
	int qry(int u, int l, int r, int L, int R) {
		if (L <= l && r <= R) return tree[u];
		int mid = l + r >> 1, res = 0;
		pushdown(u);
		if (L <= mid) res = qry(u << 1, l, mid, L, R);
		if (mid < R) reduce(res += qry(u << 1 | 1, mid + 1, r, L, R) - mod);
		return res;
	}
} pre, suc;
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i) std::cin >> A[i], rk[i] = i;
	std::sort(rk + 1, rk + 1 + n, [] (int a, int b) { return A[a] < A[b]; });
	int all = 1;
	for (int i = 1; i <= n; ++i) {
		const int u = rk[i];
		all = mul(all, B[i] = A[u] - i + 1);
		if (B[i] <= 0) { std::cout << 0 << std::endl; return 0; }
	}
	pb[0] = sb[n + 1] = 1;
	for (int i = 1; i <= n; ++i) pb[i] = mul(pb[i - 1], B[i]);
	for (int i = n; i; --i) sb[i] = mul(sb[i + 1], B[i]);
	static int tree[MAXN];
	int ans = 0;
	for (int i = 1; i <= n; ++i) {
		for (int x = rk[i]; x <= n; x += x & -x) reduce(ans += tree[x] - mod);
		for (int x = rk[i]; x; x &= x - 1) ++tree[x];
	}
	ans = mul(ans, all);
	for (int i = 1; i <= n; ++i) {
		int t = pre.qry(1, 1, n, 1, rk[i]);
		t = mul(mul(t, inv2), sb[i + 1]);
		reduce(ans += t - mod);
		t = suc.qry(1, 1, n, rk[i], n);
		t = mul(mul(t, inv2), sb[i + 1]);
		reduce(ans -= t);
		pre.mkmul(1, B[i] - 1);
		suc.mkmul(1, B[i] - 1);
		t = mul(pb[i - 1], mul(A[rk[i]] - i, A[rk[i]] - i + 1));
		pre.add(1, 1, n, rk[i], t);
		suc.add(1, 1, n, rk[i], t);
	}
	std::cout << ans << std::endl;
	return 0;
}
