#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 200005;
const int mod = 1e9 + 7;
struct mint {
  int val;
  mint() { val = 0; }
  mint(const lint& v) {
    val = (-mod <= v && v < mod) ? v : v % mod;
    if (val < 0) val += mod;
  }
  friend ostream& operator<<(ostream& os, const mint& a) { return os << a.val; }
  friend bool operator==(const mint& a, const mint& b) {
    return a.val == b.val;
  }
  friend bool operator!=(const mint& a, const mint& b) { return !(a == b); }
  friend bool operator<(const mint& a, const mint& b) { return a.val < b.val; }
  mint operator-() const { return mint(-val); }
  mint& operator+=(const mint& m) {
    if ((val += m.val) >= mod) val -= mod;
    return *this;
  }
  mint& operator-=(const mint& m) {
    if ((val -= m.val) < 0) val += mod;
    return *this;
  }
  mint& operator*=(const mint& m) {
    val = (lint)val * m.val % mod;
    return *this;
  }
  friend mint ipow(mint a, lint p) {
    mint ans = 1;
    for (; p; p /= 2, a *= a)
      if (p & 1) ans *= a;
    return ans;
  }
  friend mint inv(const mint& a) {
    assert(a.val);
    return ipow(a, mod - 2);
  }
  mint& operator/=(const mint& m) { return (*this) *= inv(m); }
  friend mint operator+(mint a, const mint& b) { return a += b; }
  friend mint operator-(mint a, const mint& b) { return a -= b; }
  friend mint operator*(mint a, const mint& b) { return a *= b; }
  friend mint operator/(mint a, const mint& b) { return a /= b; }
  operator int64_t() const { return val; }
};
int n, a[MAXN];
int val[MAXN];
mint f(int x, int y) {
  int s, e;
  if (x == y) {
    s = e = -1;
  } else {
    s = min(a[x], a[y]);
    e = max(a[x], a[y]);
  }
  mint ret = 1;
  for (int i = 1; i <= n; i++) {
    ret *= val[i] - (s < i && i <= e);
  }
  ret *= mint((mod + 1) / 2);
  return ret;
}
struct bit {
  int tree[MAXN];
  void add(int x, int v) {
    for (int i = x; i < MAXN; i += i & -i) tree[i] += v;
  }
  int query(int x) {
    int ret = 0;
    for (int i = x; i; i -= i & -i) ret += tree[i];
    return ret;
  }
} bit;
vector<int> v[MAXN];
mint pref[MAXN], suff[MAXN];
mint foo[MAXN], bar[MAXN], prec[MAXN];
mint dnc(int l, int r) {
  if (l == r) return mint(0);
  int m = (l + r) / 2;
  mint ret = dnc(l, m) + dnc(m + 1, r);
  vector<pi> L, R;
  for (int i = l; i <= m; i++) {
    for (auto& j : v[i]) {
      L.emplace_back(j, i);
    }
  }
  for (int i = m + 1; i <= r; i++) {
    for (auto& j : v[i]) {
      R.emplace_back(j, i);
    }
  }
  foo[m + 1] = pref[m];
  for (int i = m; i >= l; i--) {
    foo[i] = foo[i + 1] * prec[val[i]];
  }
  bar[m] = suff[m + 1];
  for (int i = m + 1; i <= r; i++) {
    bar[i] = bar[i - 1] * prec[val[i]];
  }
  sort((L).begin(), (L).end());
  sort((R).begin(), (R).end());
  int ptr = 0;
  mint baz = 0;
  for (int i = 0; i < ((int)(R).size()); i++) {
    while (ptr < ((int)(L).size()) && L[ptr].first <= R[i].first) {
      baz += foo[L[ptr].second + 1];
      ptr++;
    }
    ret += baz * bar[R[i].second];
  }
  return ret;
}
bool init() {
  memset(val, 0, sizeof(val));
  for (int i = 1; i <= n; i++) val[i] = 1;
  for (int i = 0; i < n; i++) val[a[i] + 1]--;
  for (int i = 1; i <= n; i++) {
    val[i] += val[i - 1];
    if (val[i] <= 0) return 1;
  }
  pref[0] = 1;
  for (int i = 1; i <= n; i++) {
    pref[i] = pref[i - 1] * mint(val[i]);
  }
  suff[n + 1] = 1;
  for (int i = n; i; i--) {
    suff[i] = suff[i + 1] * mint(val[i]);
  }
  return 0;
}
mint solve() {
  if (init()) return 0;
  for (int i = 1; i <= n; i++) v[i].clear();
  for (int i = 0; i < n; i++) {
    v[a[i]].push_back(i);
  }
  return dnc(1, n) / mint(2);
}
int main() {
  for (int i = 1; i <= n; i++) prec[i] = mint(i - 1) / mint(i);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  if (init()) {
    puts("0");
    return 0;
  }
  mint ret = 0;
  {
    mint inv = 0;
    for (int i = n - 1; i >= 0; i--) {
      inv += mint(bit.query(a[i]) + bit.query(a[i] - 1));
      bit.add(a[i], 1);
    }
    ret += inv * f(-69, -69);
  }
  ret += solve();
  reverse(a, a + n);
  ret -= solve();
  cout << ret << endl;
}
