#include <bits/stdc++.h>
using namespace std;
const long long int mod = 998244353;
const long long int big = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U>
bool mineq(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
bool maxeq(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T>
void SO(T& ve) {
  sort(ve.begin(), ve.end());
}
template <class T>
void REV(T& ve) {
  reverse(ve.begin(), ve.end());
}
template <class T>
long long int LBI(vector<T>& ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T>
long long int UBI(vector<T>& ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, i;
  cin >> n;
  vector<int> mA(n);
  for (i = 0; i < n; i++) {
    cin >> mA[i];
  }
  int stat = -1;
  for (i = 0; i < n - 1; i++) {
    if (mA[i] != 1 && mA[i + 1] == 1) {
      stat = i + 1;
      break;
    }
  }
  if (mA[n - 1] != 1 && mA[0] == 1) {
    stat = 0;
  }
  if (stat == -1) {
    if (mA[0] != 1) {
      cout << 0 << endl;
    } else {
      long long int aaa = 1;
      for (i = 1; i < n; i++) {
        aaa *= i;
        aaa %= mod;
      }
      cout << aaa << endl;
    }
    return 0;
  }
  vector<int> A(n + 1);
  for (i = 0; i < n; i++) {
    A[i] = mA[(i + stat) % n];
  }
  A[n] = A[0];
  vector<int> L(n + 1, -1);
  vector<int> R(n + 1, -1);
  for (i = 0; i < n; i++) {
    if (L[A[i]] == -1) {
      L[A[i]] = i;
    }
    R[A[i]] = i;
  }
  int kaz = 0;
  for (i = 1; i <= n; i++) {
    if (L[i] != -1) {
      kaz += R[i] + 1 - L[i];
    }
  }
  if (kaz != n) {
    cout << 0 << endl;
    return 0;
  }
  int sla = R[1] - L[1] + 1;
  long long int ans = 1;
  vector<int> ren(n, -1);
  ren[0] = sla;
  ren[sla - 1] = sla;
  int oke = 0;
  for (i = 2; i <= n; i++) {
    if (L[i] == -1) {
      ans *= oke;
      ans %= mod;
      oke--;
      continue;
    }
    int nga = R[i] - L[i] + 1;
    if (nga > sla) {
      cout << 0 << endl;
      return 0;
    }
    int zL = L[i], zR = R[i];
    if (ren[L[i] - 1] != -1) {
      oke -= max(0, ren[L[i] - 1] + 1 - sla);
      zL = L[i] - ren[L[i] - 1];
    }
    if (ren[(R[i] + 1) % n] != -1) {
      oke -= max(0, ren[(R[i] + 1) % n] + 1 - sla);
      zR = R[i] + ren[(R[i] + 1) % n];
    }
    int nn = min(n, zR - zL + 1);
    oke += max(0, nn + 1 - sla);
    if (ren[L[i] - 1] + nga == n) {
      oke = n - i + 1;
    }
    ren[(zL + n) % n] = nn;
    ren[(zR + n) % n] = nn;
    ans *= max(0, min(L[i] + sla - 1, zR) - max(R[i], zL + sla - 1) + 1);
    ans %= mod;
    oke--;
  }
  cout << ans << endl;
  return 0;
}
