#include <bits/stdc++.h>
template <typename T>
void MACRO_VAR_Scan(T& t) {
  std::cin >> t;
}
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First& first, Rest&... rest) {
  std::cin >> first;
  MACRO_VAR_Scan(rest...);
}
template <typename T>
void MACRO_VEC_ROW_Init(int n, T& t) {
  t.resize(n);
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {
  first.resize(n);
  MACRO_VEC_ROW_Init(n, rest...);
}
template <typename T>
void MACRO_VEC_ROW_Scan(int p, T& t) {
  std::cin >> t[p];
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {
  std::cin >> first[p];
  MACRO_VEC_ROW_Scan(p, rest...);
}
template <class T>
inline T CHMAX(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T>
inline T CHMIN(T& a, const T b) {
  return a = (a > b) ? b : a;
}
template <class T>
std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
  return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template <typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<ll, ll>;
using PAIRLL = std::pair<ll, ll>;
constexpr ll INFINT = 1 << 30;
constexpr ll INFINT_LIM = (1LL << 31) - 1;
constexpr ll INFLL = 1LL << 60;
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);
constexpr double EPS = 1e-10;
constexpr ll MOD = 998244353;
constexpr double PI = 3.141592653589793238462643383279;
template <class T, size_t N>
void FILL(T (&a)[N], const T& val) {
  for (auto& x : a) x = val;
}
template <class ARY, size_t N, size_t M, class T>
void FILL(ARY (&a)[N][M], const T& val) {
  for (auto& b : a) FILL(b, val);
}
template <class T>
void FILL(std::vector<T>& a, const T& val) {
  for (auto& x : a) x = val;
}
template <class ARY, class T>
void FILL(std::vector<std::vector<ARY>>& a, const T& val) {
  for (auto& b : a) FILL(b, val);
}
ll n;
PAIR getSeg(const std::vector<ll>& a) {
  if (a.size() == 0) return PAIR(-1, -1);
  if (a.size() == 1) return PAIR(a[0], a[0]);
  std::vector<ll> b;
  if (a.front() != 0 || a.back() != n - 1) b.emplace_back(0);
  for (ll i = 0; i < ll(a.size() - 1); ++i) {
    if (a[i] + 1 != a[i + 1]) b.emplace_back(i + 1);
  }
  if (b.size() == 0) return PAIR(n, n);
  if (b.size() >= 2) return PAIR(-1, -1);
  return PAIR(a[b[0]], a[(b[0] + a.size() - 1) % a.size()]);
}
bool norm(std::vector<ll>& a) {
  std::vector<ll> a0;
  for (ll i = 0; i < ll(n); ++i)
    if (a[i] == 0) a0.emplace_back(i);
  PAIR p(getSeg(a0));
  if (!(0 <= p.first && p.first < n)) return false;
  if (p.first != 0) std::rotate(a.begin(), a.begin() + p.first, a.end());
  return true;
}
ll solve(std::vector<ll> a) {
  if (!norm(a)) return 0;
  std::vector<std::vector<ll>> num(n);
  for (ll i = 0; i < ll(n); ++i) num[a[i]].emplace_back(i);
  ll cnt = num[0].size();
  ll rest = 0;
  ll ans = 1;
  for (ll x = (1); x < (n); ++x) {
    if (num[x].size()) {
      ll sz = num[x].size();
      if (sz > cnt) return 0;
      PAIR p(getSeg(num[x]));
      if (p.first == -1) return 0;
      bool existL = false, existR = false;
      if (a[p.first - 1] < x) existL = true;
      if (a[(p.second + 1) % n] < x) existR = true;
      if (!existL && !existR && sz != cnt) return 0;
      ll add = 0;
      ll coef = 1;
      if (existL && existR) {
        add = (cnt - 1) + (sz - 1);
        coef = cnt - sz + 1;
      } else if (existL ^ existR) {
        add = sz - 1;
        coef = 1;
      }
      rest += add;
      (ans *= coef) %= MOD;
    } else {
      (ans *= rest) %= MOD;
      --rest;
    }
  }
  return ans;
}
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ;
  std::cin >> n;
  std::vector<ll> a(n);
  for (auto& i : a) std::cin >> i;
  ;
  for (ll i = 0; i < ll(n); ++i) --a[i];
  std::cout << (solve(a));
  std::cout << "\n";
  ;
  return 0;
}
