#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const long long MOD = 998244353;
long long mod_pow(long long a, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 0) {
    long long tmp = mod_pow(a, n / 2);
    return (tmp * tmp) % MOD;
  }
  return (a * mod_pow(a, n - 1)) % MOD;
}
vector<long long> fact;
vector<long long> inv_fact;
void comb_init(int n) {
  fact.resize(n + 1);
  inv_fact.resize(n + 1);
  fact[0] = inv_fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    inv_fact[i] = (inv_fact[i - 1] * mod_pow(i, MOD - 2)) % MOD;
  }
}
long long comb(int n, int k) {
  if (n < k) return 0;
  long long ret = fact[n];
  (ret *= inv_fact[k]) %= MOD;
  (ret *= inv_fact[n - k]) %= MOD;
  return ret;
}
struct UnionFind {
  vector<int> par;
  vector<int> size;
  vector<int> len;
  UnionFind(int n, deque<pair<int, int> > &dq) {
    par = vector<int>(n);
    for (int i = 0; i < n; i++) par[i] = i;
    size = vector<int>(n, 1);
    len = vector<int>(n);
    for (int i = 0; i < (n); i++) len[i] = dq[i].second;
  }
  int find(int x) {
    if (x == par[x]) return x;
    return par[x] = find(par[x]);
  }
  bool same(int x, int y) { return find(x) == find(y); }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (size[x] < size[y]) {
      par[x] = y;
      size[y] += size[x];
      len[y] += len[x];
    } else {
      par[y] = x;
      size[x] += size[y];
      len[x] += len[y];
    }
  }
};
int A[300000];
int B[300000];
int appear[300001];
int count_pos(int i, int N, int X, UnionFind &U) {
  int len = U.len[U.find(i)];
  if (len == N) return N;
  return max(len - X + 1, 0);
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < (N); i++) cin >> A[i];
  int one = -1;
  for (int i = 0; i < (N); i++) {
    if (A[i] == 1) {
      one = i;
      break;
    }
  }
  if (one == -1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < (N); i++) {
    B[i] = A[(i + one) % N];
  }
  deque<pair<int, int> > dq;
  int val = B[0], len = 1;
  for (int i = 1; i < N; i++) {
    if (B[i] != val) {
      dq.emplace_back(val, len);
      val = B[i];
      len = 1;
    } else {
      len++;
    }
  }
  dq.emplace_back(val, len);
  if (dq.back().first == 1) {
    dq[0].second += dq.back().second;
    dq.pop_back();
  }
  int X = dq[0].second;
  memset(appear, -1, sizeof(appear));
  for (int i = 0; i < (dq.size()); i++) {
    if (appear[dq[i].first] != -1) {
      cout << 0 << endl;
      return 0;
    } else {
      appear[dq[i].first] = i;
    }
    if (dq[i].second > X) {
      cout << 0 << endl;
      return 0;
    }
  }
  comb_init(N);
  if (dq.size() == 1) {
    cout << fact[N] << endl;
    return 0;
  }
  UnionFind U(dq.size(), dq);
  long long ans = 1;
  int sum = 0;
  for (int i = 1; i <= N; i++) {
    if (appear[i] == -1) {
      (ans *= sum - (i - 1)) %= MOD;
    } else {
      int prv = (appear[i] + dq.size() - 1) % dq.size();
      int nxt = (appear[i] + 1) % dq.size();
      if (dq[prv].first < i && dq[nxt].first < i) {
        (ans *= (X - dq[appear[i]].second + 1)) %= MOD;
      } else if (dq[prv].first > i && dq[nxt].first > i &&
                 dq[appear[i]].second != X) {
        cout << 0 << endl;
        return 0;
      }
      sum += max(dq[appear[i]].second - X + 1, 0);
      if (dq[prv].first < i) {
        sum -= count_pos(prv, N, X, U);
        sum -= count_pos(appear[i], N, X, U);
        U.unite(prv, appear[i]);
        sum += count_pos(appear[i], N, X, U);
      }
      if (dq[nxt].first < i && !U.same(nxt, appear[i])) {
        sum -= count_pos(nxt, N, X, U);
        sum -= count_pos(appear[i], N, X, U);
        U.unite(nxt, appear[i]);
        sum += count_pos(appear[i], N, X, U);
      }
    }
  }
  cout << ans << endl;
}
