#include <bits/stdc++.h>
using namespace std;
long long pw(long long a, long long e, long long mod) {
  if (e <= 0) return 1;
  long long x = pw(a, e / 2, mod);
  x = x * x % mod;
  if (e % 2) x = x * a % mod;
  return x;
}
struct fin {
  vector<long long> T;
  long long mod;
  fin(long long mod) : mod(mod) {}
  fin(int N, long long mod) : T(N + 10, 0), mod(mod) {}
  int lastone(int x) { return x & (x ^ (x - 1)); }
  void put(int pos, long long val) {
    for (int i = pos + 1; i < (int)T.size(); i += lastone(i)) {
      T[i] += val;
      if (T[i] >= mod) T[i] -= mod;
    }
  }
  long long get(int pos) {
    long long ret = 0;
    for (int i = pos + 1; i > 0; i -= lastone(i)) {
      ret += T[i];
      if (ret >= mod) ret -= mod;
    }
    return ret;
  }
};
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> G;
  G[-1] = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    G[--A[i]] = 0;
  }
  int g = 0;
  vector<int> group_val;
  for (auto it = G.begin(); it != G.end(); it++) {
    it->second = g++;
    group_val.push_back(it->first);
  }
  if (group_val.back() != N - 1) {
    cout << "0\n";
    return 0;
  }
  g--;
  vector<int> group_size(g), group_id(N), num_pos(g, 0);
  for (int i = 0; i < N; i++) {
    group_id[i] = G[A[i]] - 1;
    num_pos[group_id[i]]++;
  }
  for (int i = 0; i < g; i++) group_size[i] = group_val[i + 1] - group_val[i];
  for (int i = g - 2; i >= 0; i--)
    num_pos[i] += num_pos[i + 1] - group_size[i + 1];
  for (int i = 0; i < g; i++)
    if (num_pos[i] < group_size[i]) {
      cout << "0\n";
      return 0;
    }
  long long cnt_tot = 1, mod = 1000000007;
  for (int i = 0; i < g; i++)
    for (int j = num_pos[i]; j > num_pos[i] - group_size[i]; j--)
      cnt_tot = cnt_tot * j % mod;
  vector<long long> inv(N + 2, 1);
  for (int i = 1; i <= N + 1; i++) inv[i] = pw(i, mod - 2, mod);
  long long ans = 0;
  vector<long long> F(g + 1, 1), fzero(g + 1, 0), Fi(g + 1);
  for (int i = 0; i < g; i++) {
    if (num_pos[i] == group_size[i])
      F[i + 1] = F[i], fzero[i + 1] = fzero[i] + 1;
    else
      F[i + 1] = F[i] * (num_pos[i] - group_size[i]) % mod,
            fzero[i + 1] = fzero[i];
    F[i + 1] = F[i + 1] * inv[num_pos[i]] % mod;
  }
  for (int i = 0; i <= g; i++) Fi[i] = pw(F[i], mod - 2, mod);
  fin Fpairs(N, mod), Fsums(N, mod);
  for (int i = N - 1; i >= 0; i--) {
    ans += Fpairs.get(group_id[i] - 1);
    Fpairs.put(group_id[i], 1);
  }
  for (int i = N - 1; i >= 0; i--) {
    ans -= Fsums.get(group_id[i] - 1) * inv[2] % mod * F[group_id[i] + 1] % mod;
    Fsums.put(group_id[i], Fi[group_id[i] + 1]);
  }
  Fsums = fin(N, mod);
  for (int i = 0; i < N; i++) {
    ans += Fsums.get(group_id[i]) * inv[2] % mod * F[group_id[i] + 1] % mod;
    Fsums.put(group_id[i], Fi[group_id[i] + 1]);
  }
  ans = ans % mod * cnt_tot % mod;
  if (ans < 0) ans += mod;
  cout << ans << "\n";
  return 0;
}
