#include <bits/stdc++.h>
using namespace std;
template <typename tp>
inline void read(tp &x) {
  x = 0;
  char c = getchar();
  int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
    ;
  if (f) x = -x;
}
namespace {
const int mo = 1e9 + 7;
inline int add(int x, int y) {
  x += y;
  return x >= mo ? x - mo : x;
}
inline int sub(int x, int y) {
  x -= y;
  return x < 0 ? x + mo : x;
}
inline int mul(int x, int y) { return (long long)x * y % mo; }
inline int power(int a, int k = mo - 2) {
  int ans = 1;
  for (; k; k >>= 1, a = mul(a, a))
    if (k & 1) ans = mul(ans, a);
  return ans;
}
inline void U(int &x, int y) { x = add(x, y); }
}  // namespace
const int N = 3e5 + 233;
int n, a[N], b[N];
inline int calc() {
  sort(b + 1, b + n + 1);
  int ans = 1;
  for (register int i = 1; i <= (int)(n); i++) ans = mul(ans, b[i] - (i - 1));
  return ans;
}
int main(void) {
  read(n);
  for (register int i = 1; i <= (int)(n); i++) read(a[i]);
  memcpy(b, a, sizeof b);
  int all = calc();
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      memcpy(b, a, sizeof b);
      if (a[i] > a[j]) {
        b[i] = b[j];
        int c = mul(power(2), calc());
        U(ans, sub(all, c));
      } else {
        b[j] = b[i];
        U(ans, mul(power(2), calc()));
      }
    }
  }
  cout << ans << "\n";
}
