#include <bits/stdc++.h>
using namespace std;
long long n, k, a[2 * 300010];
long long b[300010];
long long pl[300010];
bool used[300010];
unordered_set<long long> values[300010];
long long appcnt[300010];
long long un[300010];
long long solve() {
  cin >> n;
  k = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) k++;
  }
  if (k == 0) return 0;
  if (k == n) {
    long long ans = 1;
    for (long long i = 1; i <= n; i++) ans = ans * i % 998244353;
    ;
    return ans;
  }
  unordered_set<long long> S;
  for (int i = 0; i < n; i++) {
    if (a[i] != a[(i + 1) % n]) {
      if (S.find(a[i]) != S.end()) return 0;
      S.insert(a[i]);
    }
  }
  for (int i = 1; i <= n; i++) pl[i] = -1;
  for (int i = 0; i < n; i++) used[i] = 0;
  for (int i = 0; i < n; i++) {
    int j = (i + 1) % n;
    int id;
    if (a[i] > a[j]) {
      id = (i + k) % n;
      if (~pl[a[j]] && id != pl[a[j]]) return 0;
      pl[a[j]] = id;
      values[id].insert(a[j]);
      used[id] = 1;
    }
    if (a[i] < a[j]) {
      if (~pl[a[i]] && i != pl[a[i]]) return 0;
      pl[a[i]] = i;
      values[i].insert(a[i]);
      used[i] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (values[i].size() > 1) return 0;
  }
  for (int i = 0; i < n; i++) a[i + n] = a[i];
  deque<int> Q;
  for (int i = 0; i < 2 * n; i++) {
    while (!Q.empty()) {
      if (a[Q.back()] > a[i]) break;
      Q.pop_back();
    }
    Q.push_back(i);
    while (!Q.empty()) {
      if (Q.front() > i - k) break;
      Q.pop_front();
    }
    if (i >= n) b[i - n] = a[Q.front()];
  }
  for (int i = 1; i <= n; i++) un[i] = 0;
  for (int i = 0; i < n; i++) {
    if (used[i] == 0) {
      un[b[i]]++;
    }
  }
  long long ans = 1;
  for (int i = 1; i <= n; i++) appcnt[i] = 0;
  for (int i = 0; i < n; i++) appcnt[a[i]]++;
  for (int i = 1; i <= n; i++) {
    if (S.find(i) != S.end() && pl[i] == -1) {
      long long res = k - appcnt[i] + 1;
      if (res <= 0) return 0;
      ans = ans * res % 998244353;
      ;
      un[i]--;
      if (un[i] < 0) return 0;
    }
  }
  long long cnt = 0;
  for (int i = 1; i <= n; i++) {
    cnt += un[i];
    if (S.find(i) != S.end()) continue;
    if (cnt <= 0) return 0;
    ans = ans * cnt % 998244353;
    ;
    cnt--;
  }
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << solve() << endl;
}
