#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T& t, const U& u) {
  if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
  if (t < u) t = u;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (int i = 0; i < (v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
template <unsigned int MOD>
struct ModInt {
  using uint = unsigned int;
  using ull = unsigned long long;
  using M = ModInt;
  uint v;
  ModInt(ll _v = 0) { set_norm(_v % MOD + MOD); }
  M& set_norm(uint _v) {
    v = (_v < MOD) ? _v : _v - MOD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator+(const M& a) const { return M().set_norm(v + a.v); }
  M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }
  M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }
  M operator/(const M& a) const { return *this * a.inv(); }
  M& operator+=(const M& a) { return *this = *this + a; }
  M& operator-=(const M& a) { return *this = *this - a; }
  M& operator*=(const M& a) { return *this = *this * a; }
  M& operator/=(const M& a) { return *this = *this / a; }
  M operator-() const { return M() - *this; }
  M& operator++(int) { return *this = *this + 1; }
  M& operator--(int) { return *this = *this - 1; }
  M pow(ll n) const {
    if (n < 0) return inv().pow(-n);
    M x = *this, res = 1;
    while (n) {
      if (n & 1) res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  M inv() const {
    ll a = v, b = MOD, p = 1, q = 0, t;
    while (b != 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(p -= t * q, q);
    }
    return M(p);
  }
  bool operator==(const M& a) const { return v == a.v; }
  bool operator!=(const M& a) const { return v != a.v; }
  friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }
  static int get_mod() { return MOD; }
};
using Mint = ModInt<998244353>;
const int maxv = 300010;
V<Mint> fact(maxv), ifact(maxv), inv(maxv);
void init() {
  fact[0] = 1;
  for (int i = 1; i < maxv; ++i) {
    fact[i] = fact[i - 1] * i;
  }
  ifact[maxv - 1] = fact[maxv - 1].inv();
  for (int i = maxv - 2; i >= 0; --i) {
    ifact[i] = ifact[i + 1] * (i + 1);
  }
  for (int i = 1; i < maxv; ++i) {
    inv[i] = ifact[i] * fact[i - 1];
  }
}
Mint comb(int n, int r) {
  if (n < 0 || r < 0 || r > n) return Mint(0);
  return fact[n] * ifact[r] * ifact[n - r];
}
void ng() {
  puts("0");
  exit(0);
}
int main() {
  init();
  int N;
  cin >> N;
  V<int> A(N);
  for (int i = 0; i < (N); i++) {
    cin >> A[i];
    --A[i];
  }
  {
    int M = *max_element((A).begin(), (A).end());
    for (int i = 0; i < (N - 1); i++) {
      if (A[i] == M && A[i + 1] != M) {
        V<int> B;
        for (int j = i + 1; j < N; ++j) {
          B.push_back(A[j]);
        }
        for (int j = 0; j <= i; ++j) {
          B.push_back(A[j]);
        }
        A = B;
        break;
      }
    }
    true;
  }
  V<bool> used(N);
  V<pii> pos(N);
  int s = 0;
  int nums = 0;
  while (s < N) {
    int t = s;
    while (t < N && A[t] == A[s]) {
      ++t;
    }
    if (used[A[s]]) {
      ng();
    }
    nums++;
    pos[A[s]] = make_pair(s, t);
    used[A[s]] = true;
    s = t;
  }
  if (nums == 1) {
    if (A[0] == 1) {
      cout << fact[N] << endl;
    } else {
      ng();
    }
    return 0;
  }
  if (!used[0]) {
    ng();
  }
  int T = pos[0].second - pos[0].first;
  for (auto p : pos) {
    if (p.second - p.first > T) {
      ng();
    }
  }
  true;
  true;
  auto getpos = [&](int p) {
    if (p < 0) p += N;
    if (p >= N) p -= N;
    return p;
  };
  auto get = [&](int p) { return A[getpos(p)]; };
  auto calc = [&](pii rng) { return max(0, rng.second - rng.first - T + 1); };
  set<int> rems({-1, N});
  for (int i = 0; i < (N); i++) {
    rems.insert(i);
  }
  int free = 0;
  Mint ans = 1;
  for (int i = 0; i < (N); i++) {
    if (used[i]) {
      int l, r;
      tie(l, r) = pos[i];
      if (get(l - 1) > i && get(r) > i && r - l != T) {
        ng();
      } else if (get(l - 1) < i && get(r) < i) {
        true;
        auto itr = rems.lower_bound(r);
        int tol = *itr - T;
        true;
        int w = max(0, l - tol + 1);
        true;
        ans *= w;
      }
      for (int k = l; k < r; ++k) {
        rems.erase(k);
      }
      auto itl = rems.lower_bound(l), itr = itl;
      --itl;
      free -= calc(make_pair(*itl + 1, l));
      free -= calc(make_pair(r, *itr));
      free += calc(make_pair(*itl + 1, *itr));
    } else {
      if (rems.size() == 2) {
        ans *= (N - i);
        continue;
      }
      true;
      ans *= Mint(free - i);
    }
    true;
  }
  cout << ans << endl;
  return 0;
}
