#include <bits/stdc++.h>
using namespace std;
const int MAX = 300000;
const int MOD = 998244353;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> length(MAX + 1, 0);
  int first1 = 0, max_num = 0;
  bool start1 = false;
  for (int i = (0); i < (n); ++i) {
    cin >> a[i];
    max_num = max(max_num, a[i]);
    if (a[i] == 1) {
      ++length[1];
      if (start1) {
        first1 = i;
        start1 = false;
      }
    } else if (first1 == 0) {
      start1 = true;
    }
  }
  vector<int> b(n + 1, 0);
  int i = 0;
  for (int j = (first1); j < (n); ++j) {
    b[i] = a[j];
    ++i;
  }
  for (int j = (0); j < (first1); ++j) {
    b[i] = a[j];
    ++i;
  }
  long long ans = 1;
  vector<bool> already(MAX + 1, false);
  int nexti;
  for (int i = length[1]; i < n; i = nexti) {
    if (already[b[i]]) {
      cout << 0 << "\n";
      return 0;
    } else {
      already[b[i]] = true;
    }
    nexti = i + 1;
    while (b[nexti] == b[i]) ++nexti;
    if (nexti - i > length[1]) {
      cout << 0 << "\n";
      return 0;
    }
    length[b[i]] = nexti - i;
    if (b[i - 1] > b[i]) {
      if (b[nexti] > b[nexti - 1] && length[b[i]] != length[1]) {
        cout << 0 << "\n";
        return 0;
      }
    } else if (b[nexti] > b[nexti - 1]) {
    } else {
      ans *= (length[1] - (length[b[i]]) + 1) % MOD;
    }
  }
  for (int i = (2); i < (max_num + 1); ++i) {
    if (!already[i]) {
      bool to_next = false;
      int last = 0, temp = 0, loop_count = 0, temp_loop_count = 0;
      for (int j = 0; j < n; j += length[b[j]]) {
        if (b[j] < i) {
          if (to_next) {
            temp += length[b[j]];
            ++temp_loop_count;
          } else {
            last += length[b[j]];
            ++loop_count;
          }
        } else if (to_next) {
          last = max(last, temp);
          if (last == temp) {
            loop_count = temp_loop_count;
          }
          temp = 0;
        } else {
          to_next = true;
        }
      }
      last -= length[1] + loop_count - 1;
      last %= MOD;
      ans *= last;
    }
  }
  for (int i = n - max_num; i > 1; --i) {
    ans *= i;
    ans %= MOD;
  }
  cout << ans << "\n";
  return 0;
}
