#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
namespace fast_IO {
	const int MAXSIZE = 1 << 22;
	char buf[MAXSIZE], *p1, *p2;
	#define getchar() (p1 == p2 && (p2 = (p1 = buf) + \
			fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? EOF : *p1++)
	template <typename T>
	inline void read(T &x) {
		x = 0;
		bool sign = false;
		char ch = getchar();
		while (ch < '0' || '9' < ch) {
			sign |= ch == '-';
			ch = getchar();
		}
		while ('0' <= ch && ch <='9') {
			x = (x << 3) + (x << 1) + (ch ^ 48);
			ch = getchar();
		}
		x = sign ? -x : x;
	}
	char pbuf[MAXSIZE], *pp = pbuf;
	#define putchar(CH) putch(CH)
	inline void putch(const char &c) {
		if (pp - pbuf == MAXSIZE) {
			fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
		}
		*pp++ = c;
	}
	inline void Flush() {
		fwrite(pbuf, 1, pp - pbuf, stdout);
	}
	template <typename T>
	inline void print(T x) {
		static char outp[128];
		int tot = 0;
		if (!x) {
			putchar('0');
			return;
		} else if (x < 0) {
			x = -x;
			putchar('-');
		}
		while (x) { outp[tot++] = x % 10, x /= 10; }
		while (tot) { putchar(outp[--tot] + 48); }
	}
	inline void putstr(const string & s) {
		for (auto & ch : s) putchar(ch);
	}
	#undef getchar
	#undef putchar
}
using fast_IO :: read;
using fast_IO :: print;
using fast_IO :: Flush;
using fast_IO :: putch;
using fast_IO :: putstr;
#define read2(x, y) read(x), read(y)
#define read3(x, y, z) read2(x, y), read(z);
typedef vector<int> vi;
typedef long long lint;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef unsigned long long ulint;
#define endl '\n'
#define fst first
#define sed second
#define pb push_back
#define mp make_pair
#define rint register int
#define SZ(x) (int((x).size()))
#define all(x) (x).begin(), (x).end()
#define reveal(x) cerr << #x << " = " << (x) << endl
#define rep(it, f, e) for (rint it = (f); it <= (e); ++it)
#define per(it, f, e) for (rint it = (f); it >= (e); --it)
#define repe(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 20;
vi cant;
vector<pii> vec;
int a[MAXN], pos[MAXN], val[MAXN], n, cnt[MAXN], ep[MAXN];
lint fc[MAXN], inv[MAXN], ifc[MAXN], pval[MAXN];
lint pre[MAXN], suf[MAXN], mv[MAXN], imv[MAXN];
inline void chkMOD(int & x) {
	if (x >= MOD) x -= MOD;
	else if (x < 0) x += MOD;
}
struct Bit {
	int tr[MAXN];
	inline void clear() {
		memset(tr, 0, sizeof(tr));
	}
	inline void add(int x, int v) {
		while (x <= n) {
			chkMOD(tr[x] += v);
			x += (-x) & x;
		}
	}
	inline int sum(int x) {
		int res = 0;
		while (x) {
			chkMOD(res += tr[x]);
			x -= (-x) & x;
		}
		return res;
	}
	inline int query(int l, int r) {
		return sum(r) - sum(l - 1);
	}
}	tr, tr2;
int main() {
	read(n);
	rep (i, 1, n) {
		read(a[i]), ++cnt[a[i]];
		vec.emplace_back(a[i], i);
	}
	rep (i, 1, n) {
		cnt[i] += cnt[i - 1];
	}
	sort(all(vec));
	lint ALL = 1;
	rep (i, 0, n - 1) {
		if (vec[i].fst == i) {
			putstr("0\n");
			Flush();
			return 0;
		}
		ep[vec[i].fst] = i + 1;
		pos[vec[i].sed] = i + 1;
		pval[vec[i].fst] = vec[i].fst - i;
		(ALL *= (val[i + 1] = vec[i].fst - i)) %= MOD;
		if (vec[i].fst == i + 1) cant.emplace_back(vec[i].fst); 
	}
	sort(all(cant));
	cant.erase(unique(all(cant)), cant.end());
	auto qpow = [&] (lint x, int y) {
		lint res = 1;
		while (y) {
			if (y & 1) (res *= x) %= MOD;
			(x *= x) %= MOD, y >>= 1;
		}
		return res;
	};
	fc[1] = 1;
	rep (i, 2, n) {
		fc[i] = fc[i - 1] * i % MOD;
	}
	inv[1] = 1;
	ifc[n] = qpow(fc[n], MOD - 2);
	per (i, n - 1, 1) {
		ifc[i] = ifc[i + 1] * (i + 1) % MOD;
		inv[i + 1] = ifc[i + 1] * fc[i] % MOD;
	}
	pre[0] = 1;
	suf[n + 1] = 1;
	rep (i, 1, n) {
		pre[i] = pre[i - 1] * inv[val[i]] % MOD;
	}
	per (i, n, 1) {
		suf[i] = suf[i + 1] * inv[val[i]] % MOD;
	}
	auto getNext = [&] (int id) {
		auto it = lower_bound(all(cant), id);
		return it == cant.end() ? n : *it;
	};
	auto getLast = [&] (int id) {
		auto it = lower_bound(all(cant), id);
		return it == cant.begin() ? 0 : *(it - 1);
	};
	auto segMul = [&] (int l, int r) {
		if (l > r) return 1ll;
		return ALL * pre[l - 1] % MOD * suf[r + 1] % MOD;
	};
	auto segMul2 = [&] (int l, int r) {
		if (l > r) return 1ll;
		return mv[r] * imv[l - 1] % MOD;
	};
	auto Inv = [&] (lint x) {
		return qpow(x, MOD - 2);
	};
	mv[0] = 1;
	imv[0] = 1;
	rep (i, 1, n) {
		int mul = val[i] - 1;
		mul = max(mul, 1);
		mv[i] = mv[i - 1] * mul % MOD;
		imv[i] = imv[i - 1] + inv[mul] % MOD;
	}
	ep[0] = 0;
	lint res = 0;
	// Part 1, the count of i < j, A_i < A_j
	per (i, n, 1) {
		int L = a[i] + 1;
		int R = getNext(a[i]);
		int las = ep[getLast(a[i])] + 1;
		if (L <= R) {
			lint qry = tr.query(L, R);
			(qry *= pval[a[i]] - 1) %= MOD;
			(qry *= segMul(1, ep[a[i]])) %= MOD;
			(qry *= Inv(segMul2(las, ep[a[i]]))) %= MOD;
			(res += qry * ALL % MOD * inv[2] % MOD) %= MOD;
		}
		lint add = Inv(segMul(1, pos[i]));
		tr.add(a[i], add * segMul2(las, pos[i] - 1) % MOD);
	}
	// Part 2, the count of i < j, A_i > A_j
	tr.clear();
	tr2.clear();
	rep (i, 1, n) {
		int L = a[i] + 1;
		int R = getNext(a[i]);
		int las = ep[getLast(a[i])] + 1;
		lint NALL = tr2.query(a[i] + 1, n) * ALL % MOD;
		if (L <= R) {
			lint qry = tr.query(L, R);
			(qry *= pval[a[i]] - 1) %= MOD;
			(qry *= segMul(1, ep[a[i]])) %= MOD;
			(qry *= Inv(segMul2(las, ep[a[i]]))) %= MOD;
			(res += NALL + MOD - qry * ALL % MOD * inv[2] % MOD) %= MOD;
		} else {
			(res += NALL) %= MOD;
		}
		tr2.add(a[i], 1);
		lint add = Inv(segMul(1, pos[i]));
		tr.add(a[i], add * segMul2(las, pos[i] - 1) % MOD);
	}
	// Part 3, the count of i < j, A_i = A_j
	rep (i, 1, n) {
		int tot = cnt[i] - cnt[i - 1];
		lint prs = tot * (tot - 1) / 2;
		res += prs * ALL % MOD * inv[2] % MOD;
	}
	print(res % MOD);
	putch('\n');
	Flush();
	return 0;
}