#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <int MOD>
struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD) x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD) x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
  ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
  ModInt inverse() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      std::swap(a, b);
      u -= t * v;
      std::swap(u, v);
    }
    return ModInt(u);
  }
  bool operator==(ModInt that) const { return x == that.x; }
  bool operator!=(ModInt that) const { return x != that.x; }
  ModInt operator-() const {
    ModInt t;
    t.x = x == 0 ? 0 : Mod - x;
    return t;
  }
};
template <int MOD>
ostream &operator<<(ostream &st, const ModInt<MOD> a) {
  st << a.get();
  return st;
};
template <int MOD>
ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
  ModInt<MOD> r = 1;
  while (k) {
    if (k & 1) r *= a;
    a *= a;
    k >>= 1;
  }
  return r;
}
int N, A[601010], B[601010];
vector<int> P[301010];
int L[301010];
ModInt<998244353> solve() {
  if (count(A, A + N, 0) == 0) return 0;
  if (count(A, A + N, 0) == N) {
    ModInt<998244353> res = 1;
    for (int i = 0; i < N; i++) res *= i + 1;
    return res;
  }
  for (int i = 0; i < N; i++)
    if (A[i] == 0 and 0 < A[(i - 1 + N) % N]) {
      for (int j = 0; j < 2 * N; j++) B[j] = A[(i + j) % N];
      for (int j = 0; j < 2 * N; j++) A[j] = B[j];
      for (int j = 0; j < N; j++) P[A[j]].push_back(j);
    }
  ModInt<998244353> res = 1;
  set<int> empty, unused;
  int numfree = 0;
  empty.insert(4 * N);
  for (int i = 0; i < 2 * N; i++) empty.insert(i);
  for (int i = 0; i < N; i++) {
    L[i] = P[i].size();
    if (L[0] < L[i]) return 0;
    if (L[i] == 0) {
      res *= numfree;
      numfree--;
      continue;
    }
    if (P[i][0] + L[i] - 1 != P[i].back()) return 0;
    for (auto &p : P[i]) {
      empty.erase(p);
      empty.erase(p + N);
      unused.insert(p);
    }
    int x = P[i][0];
    if (i < A[x + N - 1] and i < A[x + L[i]] and L[i] != L[0]) return 0;
    if (A[x + N - 1] < i and A[x + L[i]] < i) res *= L[0] - L[i] + 1;
    auto it = unused.lower_bound(P[i][0] - (L[0] - 1));
    while (it != unused.end() and *it <= P[i].back()) {
      auto it2 = empty.lower_bound(*it);
      if (*it2 - *it < L[0]) {
        it = unused.lower_bound(*it2);
      } else {
        numfree++;
        it = unused.erase(it);
      }
    }
    numfree--;
  }
  return res;
}
void _main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  cout << solve() << endl;
}
