#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;
long long int N, M, K, H, W, L, R;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<int> v(N);
  vector<int> w(N);
  for (auto &i : v) cin >> i;
  bool flag = true;
  L = -1;
  for (int i = N - 1; i >= 0; i--) {
    if (v[i] == 1) {
      if (flag) {
        L = i;
      }
    } else {
      if (L != -1) flag = false;
    }
  }
  if (L == -1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    w[i] = v[(i + L) % N];
  }
  vector<int> l(N + 1, -1);
  vector<int> r(N + 1, -1);
  for (int i = 0; i < N; i++) {
    if (r[w[i]] == -1) {
      r[w[i]] = i;
      l[w[i]] = i;
    } else if (r[w[i]] == i - 1) {
      r[w[i]] = i;
    } else {
      cout << 0 << endl;
      return 0;
    }
  }
  w.push_back(1);
  long long int ans = 1;
  int used_num = r[1] + 1;
  vector<int> used(N + 1);
  int m = 0;
  for (auto i : v) m = max(m, i);
  for (int i = 0; i <= r[1]; i++) {
    used[i] = 1;
  }
  used[N] = 1;
  int num = 1;
  int cnt = 0;
  set<int> lonely;
  lonely.insert(1);
  int lonely_num = 1;
  int placed = 1;
  for (int i = 2; i <= N; i++) {
    if (l[i] == -1) {
      if (i > m) {
        ans *= N - i + 1;
        ans %= MOD;
        continue;
      }
      ans *= used_num - (r[1] - l[1]) * num - cnt - placed + lonely_num;
      ans %= MOD;
      cnt++;
      continue;
    } else if (r[i] - l[i] > r[1] - l[1]) {
      cout << 0 << endl;
      return 0;
    } else if (r[i] - l[i] < r[1] - l[1]) {
      if (w[l[i] - 1] > i && w[(r[i] + 1) % N] > i) {
        cout << 0 << endl;
        return 0;
      } else if (w[l[i] - 1] < i && w[(r[i] + 1) % N] < i) {
        ans *= r[1] - l[1] - (r[i] - l[i]) + 1;
        ans %= MOD;
      }
    }
    placed++;
    for (int j = l[i]; j <= r[i]; j++) {
      used[j] = 1;
      used_num++;
    }
    num++;
    if (lonely.find(w[l[i] - 1]) != lonely.end()) {
      lonely_num--;
      lonely.erase(w[l[i] - 1]);
    }
    if (lonely.find(w[r[i] + 1]) != lonely.end()) {
      lonely_num--;
      lonely.erase(w[r[i] + 1]);
    }
    if (!used[l[i] - 1] && !used[r[i] + 1]) {
      lonely.insert(i);
      lonely_num++;
    }
    if (used[l[i] - 1]) {
      num--;
    }
    if (used[r[i] + 1]) {
      num--;
    }
    if (i == m) num++;
  }
  cout << ans << endl;
  return 0;
}
