#include <bits/stdc++.h>
using namespace std;
long long inf;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
long long chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
long long chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T mmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T aabs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
long long dcmp(T a, T b) {
  return a > b;
}
template <long long *a>
long long cmp_a(long long first, long long second) {
  return a[first] < a[second];
}
struct __INIT__ {
  __INIT__() { memset(&inf, 0x3f, sizeof(inf)); }
} __INIT___;
namespace io {
const long long SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
long long f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char first) {
  *oS++ = first;
  if (oS == oT) flush();
}
inline bool read(signed &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(long long &first) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (first = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    first = first * 10 + (c & 15);
  first *= f;
  return 1;
}
inline bool read(char &first) {
  first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                    : *iS++);
  return first != EOF;
}
inline bool read(char *first) {
  while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                                (iS == iT ? EOF : *iS++))
                             : *iS++)) == '\n' ||
         *first == ' ' || *first == '\r')
    if (*first == EOF) return 0;
  while (!(*first == '\n' || *first == ' ' || *first == '\r'))
    *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                           : *iS++);
  *first = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
  return read(first) && read(second...);
}
inline bool write(signed first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(long long first) {
  if (!first) putc('0');
  if (first < 0) putc('-'), first = -first;
  while (first) qu[++qr] = first % 10 + '0', first /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char first) {
  putc(first);
  return 0;
}
inline bool write(const char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
inline bool write(char *first) {
  while (*first) {
    putc(*first);
    ++first;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
  return write(first) || write(second...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
pair<long long, long long> a[200005];
long long w[200005];
long long dp[200005][3];
const long long p = 1000000007;
long long inv[200005];
long long iw[200005];
long long cw[200005], cntw[200005];
struct smt {
  long long ls, rs;
  long long hv;
  long long sum, at, mt;
  smt *l, *r;
  smt(long long la, long long ra) {
    hv = 0;
    at = sum = 0;
    mt = 1;
    ls = la;
    rs = ra;
    if (ls == rs) {
      l = r = 0;
      return;
    }
    long long mid = (ls + rs) >> 1;
    l = new smt(ls, mid);
    r = new smt(mid + 1, rs);
  }
  void put_mul(long long first) {
    at = at * first % p;
    mt = mt * first % p;
    sum = sum * first % p;
  }
  void put_add(long long first) {
    at = (at + first) % p;
    sum = (sum + first * hv) % p;
  }
  void push_down() {
    l->put_mul(mt);
    l->put_add(at);
    r->put_mul(mt);
    r->put_add(at);
    mt = 1;
    at = 0;
  }
  long long Sum(long long la, long long ra) {
    if (la <= ls && rs <= ra) return sum;
    push_down();
    return (la <= l->rs ? l->Sum(la, ra) : 0) +
           (ra >= r->ls ? r->Sum(la, ra) : 0);
  }
  void upd(long long first) {
    ++hv;
    if (ls == rs) {
      sum = 0;
      return;
    }
    push_down();
    if (first <= l->rs)
      l->upd(first);
    else
      r->upd(first);
  }
};
smt *rt;
signed main() {
  long long n;
  read(n);
  for (long long i = 1; i <= n; ++i) {
    read(a[i].first);
    a[i].second = i;
  }
  inv[1] = 1;
  for (long long i = 2; i <= n; ++i) inv[i] = (p - p / i) * inv[p % i] % p;
  sort(a + 1, a + n + 1);
  long long s = 0;
  for (long long i = 1; i <= n; ++i)
    if (s >= a[i].first)
      return write("0\n");
    else {
      w[i] = a[i].first - s;
      ++s;
    }
  dp[0][0] = 1;
  for (long long i = 1; i <= n; ++i) {
    dp[i][0] = dp[i - 1][0] * w[i] % p;
    dp[i][1] = dp[i - 1][1] * (w[i] - 1) % p;
    dp[i][2] = dp[i - 1][2] * w[i] % p;
    dp[i][1] = (dp[i][1] + dp[i - 1][0] * ((w[i] - 1) * w[i] / 2 % p)) % p;
    dp[i][2] = (dp[i][2] + dp[i - 1][1]) % p;
  }
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) iw[i] = inv[w[i]] * (w[i] - 1);
  for (long long i = 1; i <= n; ++i) cw[i] = 1;
  rt = new smt(1, n);
  for (long long i = 1, l = 1; i <= n; ++i) {
    long long r = l;
    rt->put_add(1);
    while (r <= n && a[r].first == i) {
      if (a[r].second < n) {
        ans = (ans + rt->Sum(a[r].second + 1, n) * inv[w[r]]) % p;
      }
      rt->put_mul((w[r] - 1) * inv[w[r]] % p);
      ++r;
    }
    while (l < r) {
      rt->upd(a[l].second);
      ++l;
    }
  }
  for (long long i = 1; i <= n; ++i) ans = ans * w[i] % p;
  write((ans + dp[n][2]) % p, '\n');
  return 0;
}
