#include <bits/stdc++.h>
using namespace std;
long long MOD = 998244353;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ind = 0;
  for (long long i = 0; i < n; i++)
    if (a[i] == 1) {
      ind = i;
      break;
    }
  while (a[(ind - 1 + n) % n] == 1) ind--;
  if (ind < 0) ind += n;
  vector<long long> v;
  for (long long i = 0; i < n; i++) v.push_back(a[(ind + i) % n]);
  long long ans = 1, free = 0;
  long long last = v[0];
  for (long long i = 1; i < n; i++) {
    long long el = v[i];
    if (el == last)
      continue;
    else {
      if (el < last) {
        cout << 0;
        return 0;
      }
      last = el;
      continue;
    }
  }
  long long st = 0;
  long long how = 0;
  for (long long i = 0; i < n; i++)
    if (v[i] == 1) how++;
  long long ll = 0, stav = -1;
  while (st < n) {
    long long fin = st;
    while (fin < n && v[fin] == v[st]) fin++;
    if (fin - st > how) {
      cout << 0;
      return 0;
    }
    long long diff = v[st] - ll - 1;
    if (diff > free) {
      cout << 0;
      return 0;
    }
    for (long long i = 0; i < diff; i++) {
      ans *= free;
      ans %= MOD;
      free--;
    }
    long long place = fin - how;
    free += place - stav - 1;
    stav = place;
    ll = v[st];
    st = fin;
  }
  free += n - stav - 1;
  while (free) {
    ans *= free;
    ans %= MOD;
    free--;
  }
  cout << ans;
}
