#include <bits/stdc++.h>
using namespace std;
int N;
long long MOD = 998244353;
const int MAX_N = 1 << 20;
int n, dat[2 * MAX_N - 1];
void init(int n_) {
  n = 1;
  while (n < n_) n *= 2;
  for (int i = 0; i < 2 * n - 1; i++) dat[i] = INT_MAX;
}
void update(int k, int a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) return INT_MAX;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}
int A[1000001];
vector<int> last[300011];
vector<int> start[300011];
int main() {
  cin >> N;
  init(3 * N + 10);
  for (int i = 0; i < (N); i++) {
    cin >> A[i];
    A[N + i] = A[i];
    A[2 * N + i] = A[i];
    update(i, A[i]);
    update(i + N, A[i]);
    update(i + 2 * N, A[i]);
    if (i == 0) {
      A[3 * N] = A[i];
      update(i + 3 * N, A[i]);
    }
  }
  for (int i = 0; i < (N); i++) {
    if (A[N + i] != A[N + i - 1]) {
      start[A[N + i]].push_back(N + i);
    }
    if (A[N + i] != A[N + i + 1]) {
      last[A[N + i]].push_back(N + i);
    }
  }
  bool c = true;
  for (int i = 0; i < (N); i++) {
    if (A[i] != 1) c = false;
  }
  if (c) {
    long long ans = 1;
    for (int i = 0; i < (N); i++) {
      ans *= (i + 1);
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  for (int i = 0; i < (N + 1); i++) {
    if (start[i].size() > 1) {
      cout << 0 << endl;
      return 0;
    }
    if (last[i].size() > 1) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (start[1].size() != 1) {
    cout << 0 << endl;
    return 0;
  }
  int line = 0;
  for (int i = start[1][0]; i < start[1][0] + N; i++) {
    if (A[i] == 1) {
      line++;
    }
  }
  long long ans = 1;
  long long count = 0;
  long long cc = 0;
  for (int i = 0; i < (N); i++) {
    if (last[i + 1].size() == 0) {
      count++;
      ans *= cc;
      ans %= MOD;
      continue;
    }
    cc += last[i + 1][0] - start[i + 1][0];
    int now = last[i + 1][0];
    if (last[i + 1][0] < start[i + 1][0]) {
      last[i + 1][0] += N;
    }
    if (last[i + 1][0] - start[i + 1][0] + 1 > line) {
      cout << 0 << endl;
      return 0;
    }
    if (i + 1 < query(now + 1 - line, now + 1, 0, 0, n)) {
      cout << 0 << endl;
      return 0;
    }
    if (A[last[i + 1][0]] < A[last[i + 1][0] + 1]) {
      int aaa = line - (last[i + 1][0] - start[i + 1][0] + 1) + 1;
      ans *= aaa;
      ans %= MOD;
    }
  }
  cout << ans << endl;
}
