#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int a[900000];
int f[300001];
int g[300001];
int main() {
  int n, p = -1, c, num = 0, i, j, k;
  long long ans = 1;
  vector<pair<int, pair<int, int>>> v;
  set<int> s1, s2;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &a[i]);
  for (i = n; i < n * 3; i++) a[i] = a[i - n];
  if (a[0] == 1) {
    p = n;
    for (i = n - 1; i >= 0; i--) {
      if (a[i] != 1) break;
      p--;
    }
  } else {
    for (i = 0; i < n; i++) {
      if (a[i] == 1) {
        p = i;
        break;
      }
    }
  }
  if (p == -1) {
    puts("0");
    return 0;
  }
  for (i = p; i < p + n;) {
    if (f[a[i]] == 1) {
      puts("0");
      return 0;
    }
    f[a[i]] = 1;
    for (j = i; j < p + n; j++) {
      if (a[i] != a[j]) break;
    }
    if (a[i] == 1) {
      c = j - i;
    } else {
      if (j - i > c) {
        puts("0");
        return 0;
      }
    }
    v.push_back(make_pair(a[i], make_pair(i, j - i)));
    i = j;
  }
  sort(v.begin(), v.end());
  for (i = p; i < p + n; i++) s1.insert(i);
  for (i = p - n; i < p + n; i++) s2.insert(-i);
  for (i = 1, j = 0; i <= n; i++) {
    if (j == v.size() || v[j].first != i) {
      ans = ans * num % mod;
      num--;
    } else {
      int x = v[j].first;
      int y = v[j].second.first;
      int z = v[j].second.second;
      j++;
      if (s1.count(y)) {
        for (k = 0; k <= c; k++) {
          if (!s1.count(y + k)) break;
          s1.erase(y + k);
        }
      } else {
        if (x > 1 && a[y - 1] < x && a[y + z] < x)
          ans = ans * (c - z + 1) % mod;
        y = y + z - c;
        set<int>::iterator it = s1.lower_bound(y);
        if (it != s1.end()) {
          for (k = *it - y; k <= c; k++) {
            if (!s1.count(y + k)) break;
            s1.erase(y + k);
          }
        }
      }
      {
        set<int>::iterator it = s2.lower_bound(-y);
        if (it != s2.end()) {
          for (k = *it + y; k < c; k++) {
            if (!s2.count(-(y - k))) break;
            if (a[y - k + n] > x) {
              g[a[y - k + n]]++;
              num--;
            }
            s2.erase(-(y - k));
            s2.erase(-(y - k + n));
            s2.erase(-(y - k - n));
            num++;
          }
        }
        num--;
      }
      num += g[x];
    }
  }
  printf("%lld\n", ans);
  return 0;
}
