#include <bits/stdc++.h>
static const long long int mod = 1000000007LL;
long long int exgcd(long long int a, long long int b, long long int &c,
                    long long int &d) {
  if (b == 0) {
    c = 1, d = 0;
    return a;
  }
  long long int x = exgcd(b, a % b, d, c);
  d -= c * (a / b);
  return a;
}
long long int finv(long long int x) {
  long long int c, d;
  exgcd(x, mod, c, d);
  if (c < 0) c += mod;
  return c;
}
int a[200003];
int n;
int cnt[200003];
struct pair {
  int a, b;
  pair() : a(0), b(0) {}
  pair(int a, int b) : a(a), b(b) {}
  pair operator+(const pair &c) const {
    if (b > c.b || c.a == 0) return *this;
    if (c.b > b || a == 0) return c;
    return pair((a + c.a) % mod, b);
  }
  pair operator*(const pair &c) const {
    return pair((long long int)a * c.a % mod, b + c.b);
  }
} arr[200003];
void add(int i, pair v) {
  while (i <= n) {
    arr[i] = arr[i] + v;
    i += i & -i;
  }
}
pair sum(int i) {
  pair ans = arr[i];
  i -= i & -i;
  while (i > 0) {
    ans = ans + arr[i];
    i -= i & -i;
  }
  return ans;
}
int arr2[200003];
void add2(int i, int v) {
  while (i <= n) {
    arr2[i] = arr2[i] + v;
    i += i & -i;
  }
}
int sum2(int i) {
  int ans = 0;
  while (i > 0) {
    ans += arr2[i];
    i -= i & -i;
  }
  return ans;
}
long long int inv[200003];
std::pair<pair, pair> calpair(int k) {
  pair a = k == 0 ? pair(1, -1) : pair(k, 0);
  pair b = k - 1 == 0 ? pair(1, -1) : pair(k - 1, 0);
  return std::make_pair(pair(a.a * inv[b.a] % mod, a.b - b.b),
                        pair(b.a * inv[a.a] % mod, b.b - a.b));
}
pair suf1[200003], suf2[200003];
pair pre1[200003], pre2[200003];
int main() {
  scanf("%d", &n);
  inv[1] = 1;
  for (int i = 2; i <= n; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    --a[i];
    ++cnt[a[i]];
  }
  for (int i = n - 2; i >= 0; --i) {
    cnt[i] += cnt[i + 1] - 1;
  }
  long long int ians = 1;
  for (int i = 0; i < n; ++i) {
    if (cnt[i] <= 0) {
      printf("0\n");
      return 0;
    }
    ians = ians * cnt[i] % mod;
  }
  for (int i = n - 1; i >= 0; --i) {
    std::pair<pair, pair> p = calpair(cnt[i]);
    if (i == n - 1) {
      suf1[i] = p.first;
      suf2[i] = p.second;
    } else {
      suf1[i] = p.first * suf1[i + 1];
      suf2[i] = p.second * suf2[i + 1];
    }
  }
  for (int i = 0; i < n; ++i) {
    std::pair<pair, pair> p = calpair(cnt[i]);
    if (i == 0) {
      pre1[i] = p.first;
      pre2[i] = p.second;
    } else {
      pre1[i] = p.first * pre1[i - 1];
      pre2[i] = p.second * pre2[i - 1];
    }
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    pair val = sum(a[i] + 1) * pre2[a[i]];
    assert(val.b <= 0);
    if (val.b == 0) ans += val.a;
    add(a[i] + 1, pre1[a[i]]);
  }
  memset(arr, 0, sizeof(arr));
  int count = 0;
  for (int i = n - 1; i >= 0; --i) {
    pair val = sum(a[i]) * pre2[a[i]];
    count += sum2(a[i]);
    assert(val.a == 0 || val.b <= 0);
    if (val.b == 0) ans -= val.a;
    add(a[i] + 1, pre1[a[i]]);
    add2(a[i] + 1, 1);
  }
  ans = ans * inv[2] % mod;
  ans = ians * (ans + count) % mod;
  if (ans < 0) ans += mod;
  printf("%lld\n", ans);
}
