#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
template <unsigned MOD_>
struct ModInt {
  static const unsigned MOD = MOD_;
  unsigned x;
  void undef() { x = (unsigned)-1; }
  bool isnan() const { return x == (unsigned)-1; }
  inline int geti() const { return (int)x; }
  ModInt() { x = 0; }
  ModInt(const ModInt &y) { x = y.x; }
  ModInt(int y) {
    if (y < 0 || (int)MOD <= y) y %= (int)MOD;
    if (y < 0) y += MOD;
    x = y;
  }
  ModInt(unsigned y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  ModInt(long long y) {
    if (y < 0 || MOD <= y) y %= MOD;
    if (y < 0) y += MOD;
    x = y;
  }
  ModInt(unsigned long long y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  ModInt &operator+=(const ModInt y) {
    if ((x += y.x) >= MOD) x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt y) {
    if ((x -= y.x) & (1u << 31)) x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt y) {
    x = (unsigned long long)x * y.x % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt y) {
    x = (unsigned long long)x * y.inv().x % MOD;
    return *this;
  }
  ModInt operator-() const { return (x ? MOD - x : 0); }
  ModInt inv() const { return pow(MOD - 2); }
  ModInt pow(long long y) const {
    ModInt b = *this, r = 1;
    if (y < 0) {
      b = b.inv();
      y = -y;
    }
    for (; y; y >>= 1) {
      if (y & 1) r *= b;
      b *= b;
    }
    return r;
  }
  ModInt extgcd() const {
    unsigned a = MOD, b = x;
    int u = 0, v = 1;
    while (b) {
      int t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if (u < 0) u += MOD;
    return ModInt(u);
  }
  friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
  friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
  friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
  friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
  friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
  friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
  friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
const long long MOD = 998244353;
ModInt<MOD> fact(int n) {
  ModInt<MOD> r = 1;
  for (int i = 0, i_len = (n); i < i_len; ++i) r *= i + 1;
  return r;
}
int N;
int A[300011];
int B[300011];
int buf[300011];
int L[300011], R[300011];
int C[300011];
int POS[300011];
int put[300011];
int step;
ModInt<MOD> calc() {
  int prv = A[N - 1];
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    if (prv != A[i]) {
      if (L[A[i]]) return 0;
      POS[A[i]] = i;
      L[A[i]] = prv;
      R[prv] = A[i];
    }
    prv = A[i];
  }
  ModInt<MOD> ans = 1;
  B[N] = 1;
  int fr = 0;
  for (int i = 1; i <= N; i++) {
    const int pos = POS[i];
    if (C[i] == 0) {
      ans *= fr;
      fr--;
    } else if (L[i] > i) {
      put[i] = pos;
      for (int t = 0, t_len = (step); t < t_len; ++t) {
        if (B[pos + t]) {
          if (t != C[i]) return 0;
          break;
        } else if (t > C[i]) {
          return 0;
        }
        B[pos + t] = i;
      }
      if (R[i] < i) fr += (R[i] != 1 ? put[R[i]] : N) - put[i] - 1;
    } else if (i < R[i]) {
      put[i] = pos + C[i] - step;
      for (int t = 0, t_len = (step); t < t_len; ++t) {
        if (B[pos + C[i] - 1 - t]) {
          if (t != C[i]) return 0;
          break;
        } else if (t > C[i]) {
          return 0;
        }
        B[pos + C[i] - 1 - t] = i;
      }
      if (L[i] < i) fr += put[i] - put[L[i]] - 1;
    } else {
      for (int t = 0, t_len = (step); t < t_len; ++t) {
        if (B[pos + t]) {
          if (t != C[i]) return 0;
          break;
        } else if (t > C[i]) {
          return 0;
        }
        B[pos + t] = i;
      }
      ans *= step - C[i] + 1;
      fr += (R[i] != 1 ? put[R[i]] : N) - put[L[i]] - 2;
    }
  }
  return ans;
}
void MAIN() {
  scanf("%d", &N);
  for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", A + i);
  ModInt<MOD> ans = 0;
  int prv = A[N - 1];
  int pos = -1;
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    if (A[i] == 1 && prv != 1) {
      pos = i;
      break;
    }
    prv = A[i];
  }
  if (pos == -1) {
    ans = 0;
  } else {
    for (int i = 0, i_len = (N); i < i_len; ++i) {
      buf[i] = A[(i + pos) % N];
    }
    for (int i = 0, i_len = (N); i < i_len; ++i) A[i] = buf[i];
    for (int i = 0, i_len = (N); i < i_len; ++i) C[A[i]]++;
    step = C[1];
    if (step == N) {
      ans = fact(N);
    } else if (step < *max_element(C, C + N + 1)) {
      ans = 0;
    } else {
      ans = calc();
    }
  }
  printf("%d\n", ans.geti());
}
int main() {
  int TC = 1;
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}
