#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long LEFT[500000];
long long RIGHT[500000];
long long N;
long long A[500000];
long long inv[1000000];
long long FactorialInv[1000000];
long long Factorial[1000000];
long long beki(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  long long ans = beki(a, b / 2);
  ans = ans * ans % mod;
  if (b % 2 == 1) {
    ans = ans * a % mod;
  }
  return ans;
}
void init_combination() {
  inv[1] = 1;
  FactorialInv[1] = 1;
  Factorial[1] = 1;
  for (int i = 2; i < 1000000; i++) {
    inv[i] = beki(i, mod - 2);
    Factorial[i] = Factorial[i - 1] * i % mod;
    FactorialInv[i] = FactorialInv[i - 1] * inv[i] % mod;
  }
}
long long combination(long long a, long long b) {
  if ((a == b) || (b == 0)) {
    return 1;
  }
  long long ans = Factorial[a] * FactorialInv[b] % mod;
  ans = ans * FactorialInv[a - b] % mod;
  return ans;
}
int main() {
  init_combination();
  cin >> N;
  long long maxi = 0;
  for (int i = 1; i <= N; i++) {
    LEFT[i] = RIGHT[i] = -1;
    cin >> A[i];
    maxi = max(A[i], maxi);
  }
  if (maxi == 1) {
    cout << Factorial[N] << endl;
    return 0;
  }
  int index = -1;
  for (int i = 1; i <= N; i++) {
    if (A[i] == 1) {
      index = i;
      break;
    }
  }
  if (index == -1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i < index; i++) {
    A[i + N] = A[i];
  }
  for (int i = 1; i <= N; i++) {
    A[i] = A[i + index - 1];
  }
  index = N;
  while (A[index] == 1) index--;
  for (int i = N; i - N + index >= 1; i--) {
    A[i] = A[i - (N - index)];
  }
  for (int i = 1; i <= (N - index); i++) {
    A[i] = 1;
  }
  for (int i = 1; i <= N; i++) {
  }
  LEFT[1] = 1;
  A[N + 1] = 0;
  A[0] = 0;
  LEFT[0] = -1;
  for (int i = 2; i <= N + 1; i++) {
    if (A[i] == A[i - 1]) continue;
    RIGHT[A[i - 1]] = i - 1;
    if (LEFT[A[i]] != -1) {
      cout << 0 << endl;
      return 0;
    }
    LEFT[A[i]] = i;
  }
  for (int i = 1; i <= N; i++) {
  }
  long long TIMES = RIGHT[1] - LEFT[1] + 1;
  long long ans = 1;
  long long zero_num = 0;
  long long ok = 0;
  for (int i = 2; i <= N; i++) {
    if (LEFT[i] == -1) {
      ans = (ans * ok) % mod;
      ok--;
      continue;
    }
    if ((RIGHT[i] - LEFT[i] + 1 > TIMES)) {
      cout << "0" << endl;
      return 0;
    }
    long long Lvalue = A[LEFT[i] - 1];
    long long Rvalue = A[RIGHT[i] + 1];
    long long delta = RIGHT[i] - LEFT[i] + 1;
    if (Lvalue < i) ok += delta;
    if (Rvalue < i) ok += delta;
    ok--;
    if ((RIGHT[i] - LEFT[i] + 1 == TIMES)) {
      ok--;
      continue;
    }
    if ((Lvalue > i) && (Rvalue > i)) {
      cout << 0 << endl;
      return 0;
    }
    if ((Lvalue < i) && (Rvalue < i)) {
      ans = (ans * (TIMES - delta + 1)) % mod;
    }
  }
  cout << ans << endl;
  return 0;
}
