#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A& a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A& a, B b) {
  if (a < b) a = b;
}
template <class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
  ost << "{" << p.first << "," << p.second << "}";
  return ost;
}
template <class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
  ost << "{";
  for (long long i = 0; i < v.size(); i++) {
    if (i) ost << ",";
    ost << v[i];
  }
  ost << "}";
  return ost;
}
template <uint32_t mod>
struct ModInt {
  uint32_t a;
  ModInt& s(uint32_t vv) {
    a = vv < mod ? vv : vv - mod;
    return *this;
  }
  ModInt(int64_t x = 0) { s(x % mod + mod); }
  ModInt& operator+=(const ModInt& x) { return s(a + x.a); }
  ModInt& operator-=(const ModInt& x) { return s(a + mod - x.a); }
  ModInt& operator*=(const ModInt& x) {
    a = uint64_t(a) * x.a % mod;
    return *this;
  }
  ModInt& operator/=(const ModInt& x) {
    *this *= x.inv();
    return *this;
  }
  ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }
  bool operator==(const ModInt& x) const { return a == x.a; }
  bool operator!=(const ModInt& x) const { return a != x.a; }
  bool operator<(const ModInt& x) const { return a < x.a; }
  ModInt operator-() const { return ModInt() - *this; }
  ModInt pow(int64_t n) const {
    ModInt res(1), x(*this);
    while (n) {
      if (n & 1) res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  ModInt inv() const { return pow(mod - 2); }
};
template <uint32_t mod>
istream& operator>>(istream& in, const ModInt<mod>& a) {
  return (in >> a.a);
}
template <uint32_t mod>
ostream& operator<<(ostream& out, const ModInt<mod>& a) {
  return (out << a.a);
}
using mint = ModInt<1000000007>;
template <class T>
struct BinaryIndexedTree {
  long long n;
  vector<T> dat;
  BinaryIndexedTree(long long n = 0) : n(n) { dat.resize(n + 1); }
  void add(long long k, T x) {
    for (k++; k <= n; k += k & -k) dat[k] += x;
  }
  T sum(long long k) {
    T ret = 0;
    for (k++; k; k -= k & -k) ret += dat[k];
    return ret;
  }
};
long long N;
long long A[666666];
signed main() {
  cin >> N;
  for (long long i = 0; i < (N); i++) cin >> A[i];
  for (long long i = 0; i < (N); i++) {
    if (A[i] == 1 && A[i] != A[(i + N - 1) % N]) {
      rotate(A, A + i, A + N);
      break;
    }
  }
  if (A[0] != 1) {
    cout << 0 << endl;
    return 0;
  }
  long long K = 0;
  while (K < N && A[K] == 1) K++;
  if (K == N) {
    mint ans = 1;
    for (long long i = 0; i < (N); i++) ans *= i + 1;
    cout << ans << endl;
    return 0;
  }
  vector<pair<long long, long long> > lis(N + 1,
                                          pair<long long, long long>(-1, -1));
  long long cur = 0;
  while (cur < N) {
    long long nex = cur;
    while (nex < N && A[cur] == A[nex]) nex++;
    if (lis[A[cur]].first != -1) {
      cout << 0 << endl;
      return 0;
    }
    if (nex - cur > K) {
      cout << 0 << endl;
      return 0;
    }
    lis[A[cur]] = pair<long long, long long>(cur, nex);
    cur = nex;
  }
  deque<pair<long long, long long> > deq;
  vector<vector<long long> > add(N + 114);
  for (long long i = 0; i < N + K - 1; i++) {
    while (deq.size() && deq.back().first < A[i]) deq.pop_back();
    if (deq.size() && deq.front().second + K <= i) deq.pop_front();
    deq.emplace_back(A[i], i);
    if (i - K + 1 >= 0) {
      add[deq.front().first + 1].push_back(i - K + 1);
    }
  }
  set<long long> used;
  set<long long> cand;
  mint ans = 1;
  for (long long i = 1; i <= N; i++) {
    for (auto a : add[i])
      if (used.find(a) == used.end()) cand.insert(a);
    if (lis[i].first == -1) {
      ans *= cand.size();
      cand.erase(*cand.begin());
      continue;
    }
    long long l, r;
    tie(l, r) = lis[i];
    if (A[r % N] > i) {
      used.insert(r - K);
      continue;
    }
    if (A[(l - 1 + N) % N] > i) {
      used.insert(l);
      continue;
    }
    long long al = r - K;
    long long ar = l + 1;
    ans *= ar - al;
    used.insert(l);
  }
  cout << ans << endl;
}
