#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-9;
const long long mod = 998244353;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
class Union_Find_Tree {
 public:
  vector<int> p, r, s;
  Union_Find_Tree(int n) {
    p = r = vector<int>(n);
    s = vector<int>(n, 1);
    for (int i = 0; i < n; i++) p[i] = i;
  }
  int Par(int x) {
    if (p[x] == x) return x;
    return p[x] = Par(p[x]);
  }
  int Size(int x) { return s[Par(x)]; }
  bool Unite(int x, int y) {
    x = Par(x);
    y = Par(y);
    if (x == y) return 0;
    if (r[x] < r[y]) {
      p[x] = y;
      s[y] += s[x];
    } else {
      p[y] = x;
      s[x] += s[y];
      if (r[x] == r[y]) r[x]++;
    }
    return 1;
  }
  bool Same(int x, int y) { return Par(x) == Par(y); }
};
int n;
vector<int> a;
int main() {
  cin >> n;
  a = vector<int>(n);
  deque<int> q(n);
  bool B = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    q[i] = a[i];
    if (!a[i]) B = 1;
  }
  if (a == vector<int>(n, 0)) {
    long long res = 1;
    for (long long i = 1; i <= n; i++) (res *= i) %= mod;
    cout << res << endl;
    return 0;
  }
  if (!B) {
    cout << 0 << endl;
    return 0;
  }
  while (q[0] != 0 || q[n - 1] == 0) {
    int tmp = q[n - 1];
    q.push_front(tmp);
    q.pop_back();
  }
  for (int i = 0; i < n; i++) a[i] = q[i];
  vector<int> L, R;
  L = R = vector<int>(n, -1);
  long long t = -1;
  for (int i = 0; i < n; i++) {
    int A = a[i];
    if (t != A && L[A] != -1) {
      cout << 0 << endl;
      return 0;
    }
    if (L[A] == -1) {
      if (t != -1) R[t] = i - 1;
      t = A;
      L[A] = i;
    }
  }
  R[t] = n - 1;
  long long k = R[0] - L[0], res = 1;
  t = 0;
  int N = 0;
  vector<int> c(n);
  Union_Find_Tree uft(n);
  for (int i = 0; i < n; i++) {
    if (L[i] == -1) {
      if (N < n)
        (res *= max(0ll, t - i)) %= mod;
      else
        (res *= n - i) %= mod;
      continue;
    }
    int K = R[i] - L[i];
    if (K > k) res = 0;
    for (int j = L[i]; j < R[i]; j++) {
      uft.Unite(j, j + 1);
      c[j] = c[j + 1] = 1;
    }
    N += K + 1;
    int flag = 0;
    if (L[i] - 1 >= 0 && c[L[i] - 1]) {
      t -= uft.Size(L[i] - 1) - k;
      uft.Unite(L[i] - 1, L[i]);
      flag++;
    }
    if (c[(R[i] + 1) % n]) {
      t -= uft.Size((R[i] + 1) % n) - k;
      uft.Unite(R[i], (R[i] + 1) % n);
      flag++;
    }
    if (flag == 2) (res *= k - K + 1) %= mod;
    t += uft.Size(L[i]) - k;
    if (N == n) t = n;
  }
  cout << res << endl;
}
