#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
const long long MOD = 1000000007;
const long long MAX = 4000001;
const double eps = 1E-18;
long long max(long long a, long long b) {
  if (a > b) {
    return a;
  }
  return b;
}
long long min(long long a, long long b) {
  if (a > b) {
    return b;
  }
  return a;
}
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct edge {
  long long ind;
  long long fr;
  long long to;
  long long d;
};
class mint {
  long long x;

 public:
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint& a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint& a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint& a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint& a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint& a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint& operator/=(const mint& a) { return (*this) *= a.inv(); }
  mint operator/(const mint& a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream& operator<<(ostream& os, const mint& m) {
    os << m.x;
    return os;
  }
};
mint pw(mint a, long long b) {
  if (b == 0) {
    return 1;
  }
  mint ret = pw(a, b >> 1);
  ret *= ret;
  if (b & 1) {
    ret *= a;
  }
  return ret;
}
template <typename T>
struct Segment_Tree {
  typedef function<T(T, T)> F;
  long long n;
  F f;
  T unit;
  long long size;
  vector<T> seg;
  Segment_Tree(long long n0, F f0, T unit0) {
    n = n0;
    f = f0;
    unit = unit0;
    size = 1;
    while (size < n) {
      size *= 2;
    }
    seg.assign(2 * size, unit);
  }
  void update(long long k, T x) {
    k += size - 1;
    seg[k] = x;
    while (k > 1) {
      k /= 2;
      seg[k] = f(seg[2 * k], seg[2 * k + 1]);
    }
  }
  T query_prep(long long a, long long b, long long k, long long l,
               long long r) {
    if (b <= l || a >= r) {
      return unit;
    }
    if (a <= l && r <= b) {
      return seg[k];
    }
    T v1 = query_prep(a, b, 2 * k, l, (l + r) / 2);
    T v2 = query_prep(a, b, 2 * k + 1, (l + r) / 2, r);
    return f(v1, v2);
  }
  T query(long long a, long long b) { return query_prep(a, b, 1, 1, size + 1); }
};
int main() {
  long long N;
  cin >> N;
  vector<long long> a(N);
  for (long long i = 0; i < N; i++) cin >> a[i];
  long long c = 0;
  for (long long i = 1; i <= N - 1; i++) {
    if (a[i - 1] != 1 && a[i] == 1) {
      c = i;
    }
  }
  vector<long long> b(N);
  for (long long i = 0; i < N; i++) b[i] = a[(i + c) % N];
  long long K = 0;
  for (long long i = 0; i < N; i++)
    if (b[i] == 1) {
      K++;
    }
  for (long long i = 0; i < K; i++) {
    if (b[i] != 1) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (long long i = 0; i < N; i++) a[i] = b[i];
  vector<vector<long long>> lis(N + 1);
  for (long long i = 0; i < N; i++) {
    if (!lis[a[i]].empty() && *(--lis[a[i]].end()) != i - 1) {
      cout << 0 << endl;
      return 0;
    }
    lis[a[i]].push_back(i);
  }
  set<long long> st;
  for (long long i = 1; i <= N - 1; i++) st.insert(i);
  Segment_Tree<long long> sg(
      N, [](long long a, long long b) { return a + b; }, 0);
  mint ans = 1;
  for (long long i = 2; i <= N; i++) {
    if (lis[i].empty()) {
      continue;
    }
    long long l = lis[i][0];
    long long r = lis[i][lis[i].size() - 1];
    if (lis[i].size() > K) {
      cout << 0 << endl;
      return 0;
    }
    long long x;
    if (a[l - 1] > a[l] && r < N - 1 && a[r] < a[r + 1]) {
      if (lis[i].size() != K) {
        cout << 0 << endl;
        return 0;
      }
      x = l;
    } else if (a[l - 1] > a[l]) {
      x = l;
    } else if (r < N - 1 && a[r] < a[r + 1]) {
      x = r - K + 1;
    } else {
      auto itr = st.lower_bound(r - K + 1);
      x = *itr;
      if (x > l) {
        cout << 0 << endl;
        return 0;
      }
      long long y = (K + l - r) - sg.query(r - K + 1, l + 1);
      ans *= y;
    }
    if (sg.query(x, x + 1) == 1) {
      cout << 0 << endl;
      return 0;
    }
    st.erase(x);
    sg.update(x, 1);
  }
  long long rem = 0;
  for (long long i = N; i >= 1; i--) {
    if (lis[i].size() == 0) {
      rem++;
      continue;
    }
    long long l = lis[i][0];
    long long r = lis[i][lis[i].size() - 1];
    for (long long j = r; j >= max(l - K + 1, 1); j--) {
      if (sg.query(j, j + 1) == 0) {
        ans *= rem;
        rem--;
      }
      if (sg.query(j, j + 1) == 2 && j < l) {
        break;
      }
      sg.update(j, 2);
    }
  }
  cout << ans << endl;
}
