#include <bits/stdc++.h>
using namespace std;
vector<int> v, w;
int a[300010], cnt[300010] = {}, s[2][300010], p[300010] = {};
long long ans = 1, mod = 998244353;
int main() {
  int i, j, n;
  cin >> n;
  for (i = 1; i <= n; i++) {
    s[0][i] = -1;
    s[1][i] = -1;
  }
  for (i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  j = 0;
  for (i = 0; i < n; i++) {
    if (v.back() == v[0]) {
      w.push_back(v.back());
      v.pop_back();
    } else {
      w.push_back(j);
      j++;
    }
  }
  v.clear();
  for (i = 1; i <= n; i++) {
    a[i] = w[i - 1];
  }
  w.clear();
  for (i = 1; i <= n; i++) {
    cnt[a[i]]++;
    if (s[0][a[i]] == -1) s[0][a[i]] = i;
    if (s[1][a[i]] != -1 && s[1][a[i]] != i - 1) ans = 0;
    s[1][a[i]] = i;
  }
  int k = cnt[1];
  if (k == n) {
    for (i = 1; i <= n; i++) {
      (ans *= i) %= mod;
    }
    cout << ans << endl;
    return 0;
  }
  if (k == 1) {
    for (i = 1; i <= n; i++) {
      if (cnt[i] != 1) {
        cout << 0 << endl;
        return 0;
      }
    }
    cout << n << endl;
    return 0;
  }
  for (i = n - k + 1; i <= n; i++) {
    if (cnt[i]) ans = 0;
  }
  int c = 0;
  for (i = 1; i <= n; i++) {
    if (cnt[i] > k) {
      ans = 0;
    } else if (cnt[i] == k) {
      int x = s[0][a[i]];
      for (j = 0; j < k; j++) {
        p[x + j] = i;
      }
      c += k;
    } else {
      if (cnt[i] == 0) {
        (ans *= (c - i)) %= mod;
      } else {
        int x = k;
        if (a[s[0][a[i]] - 1] > i && a[(s[0][a[i]] + 1) % (n + 1)] > i)
          x = 0;
        else if (a[s[0][a[i]] - 1] > i)
          x = 1;
        else if (a[(s[1][a[i]] + 1) % (n + 1)] > i)
          x = 1;
        (ans *= x) %= mod;
      }
    }
  }
  cout << ans << endl;
}
