#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i != v.size() - 1) cout << endl;
  };
  return os;
}
template <typename T>
void Out(T x) {
  cout << x << endl;
}
template <typename T1, typename T2>
void Ans(bool first, T1 y, T2 n) {
  if (first)
    Out(y);
  else
    Out(n);
}
using vec = vector<long long>;
using mat = vector<vec>;
using Pii = pair<long long, long long>;
using PiP = pair<long long, Pii>;
using PPi = pair<Pii, long long>;
using bools = vector<bool>;
using pairs = vector<Pii>;
const long long mod = 998244353;
template <long long MOD>
struct ModInt {
  using ll = long long;
  ll val;
  void setval(ll v) { val = v % MOD; };
  ModInt() : val(0) {}
  ModInt(ll v) { setval(v); };
  ModInt operator+(const ModInt &x) const { return ModInt(val + x.val); }
  ModInt operator-(const ModInt &x) const { return ModInt(val - x.val + MOD); }
  ModInt operator*(const ModInt &x) const { return ModInt(val * x.val); }
  ModInt operator/(const ModInt &x) const { return *this * x.inv(); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt operator+=(const ModInt &x) { return *this = *this + x; }
  ModInt operator-=(const ModInt &x) { return *this = *this - x; }
  ModInt operator*=(const ModInt &x) { return *this = *this * x; }
  ModInt operator/=(const ModInt &x) { return *this = *this / x; }
  friend ostream &operator<<(ostream &os, const ModInt &x) {
    os << x.val;
    return os;
  }
  friend istream &operator>>(istream &is, ModInt &x) {
    is >> x.val;
    x.val = (x.val % MOD + MOD) % MOD;
    return is;
  }
  ModInt pow(ll n) const {
    ModInt a = 1;
    if (n == 0) return a;
    long long i0 = 64 - __builtin_clzll(n);
    for (long long i = i0 - 1; i >= 0; i--) {
      a = a * a;
      if ((n >> i) & 1) a *= (*this);
    }
    return a;
  }
  ModInt inv() const { return this->pow(MOD - 2); }
};
using mint = ModInt<mod>;
mint pow(mint x, long long n) { return x.pow(n); }
using mvec = vector<mint>;
using mmat = vector<mvec>;
struct Combination {
  vector<mint> fact, invfact;
  Combination(long long N) {
    fact = vector<mint>({mint(1)});
    invfact = vector<mint>({mint(1)});
    fact_initialize(N);
  }
  void fact_initialize(long long N) {
    long long i0 = fact.size();
    if (i0 >= N + 1) return;
    fact.resize(N + 1);
    invfact.resize(N + 1);
    for (long long i = i0; i <= N; i++) fact[i] = fact[i - 1] * i;
    invfact[N] = (mint)1 / fact[N];
    for (long long i = N - 1; i >= i0; i--)
      invfact[i] = invfact[i + 1] * (i + 1);
  }
  mint nCr(long long n, long long r) {
    if (n < 0 || r < 0 || r > n) return mint(0);
    if (fact.size() < n + 1) fact_initialize(n);
    return fact[n] * invfact[r] * invfact[n - r];
  }
  mint nPr(long long n, long long r) {
    if (n < 0 || r < 0 || r > n) return mint(0);
    if (fact.size() < n + 1) fact_initialize(n);
    return fact[n] * invfact[n - r];
  }
};
signed main() {
  long long N;
  cin >> N;
  vec a(N);
  cin >> a;
  Combination C(N);
  long long i0 = -1;
  for (long long i = (0); i < (N); i++) {
    if (a[i] == 1)
      i0 = i;
    else if (a[0] == 1)
      break;
  }
  if (i0 == -1) {
    Out(0);
    return 0;
  } else if (a[0] == 1 && i0 == N - 1) {
    Out(C.fact[N]);
    return 0;
  }
  vec A(N);
  for (long long i = (0); i < (N); i++) A[i] = a[(i0 - i + N) % N] - 1;
  long long n = 0;
  for (long long i = (0); i < (N); i++)
    if (A[i] == 0) n++;
  A.push_back(0);
  vec c(N, -1000000000000000000);
  c[0] = n;
  long long t = 1, prev = 0;
  mint ans = 1;
  for (long long i = (n); i < (N); i++) {
    if (c[A[i]] > -1000000000000000000) {
      Out(0);
      return 0;
    }
    if (A[i] != A[i + 1]) {
      if (t > n) {
        Out(0);
        return 0;
      }
      long long next = A[i + 1];
      if (prev < A[i] && A[i] > next) {
        ans *= n - t + 1;
        c[A[i]] = t + n - 1;
        c[prev] = 1;
      } else if (prev < A[i] && A[i] < next) {
        c[A[i]] = n;
        c[prev] -= n - t;
      } else if (prev > A[i] && A[i] > next) {
        c[A[i]] = t;
      } else if (prev > A[i] && A[i] < next) {
        if (t != n) {
          Out(0);
          return 0;
        }
        c[A[i]] = n;
      }
      t = 1;
      prev = A[i];
    } else
      t++;
  }
  long long U = 0;
  for (long long i = (N)-1; i >= (0); i--)
    if (c[i] > -1000000000000000000) {
      if (c[i] <= 0)
        ans = 0;
      else {
        ans *= C.nPr(N - 1 - i - U, c[i] - 1);
        U += c[i];
      }
    }
  assert(ans.val == 0);
  Ans(U == N, ans, 0);
  return 0;
}
