#include <bits/stdc++.h>
template <uint32_t Modulus>
class IntMod {
 public:
  template <uint32_t Modulus_>
  friend bool operator==(const IntMod<Modulus_>& left,
                         const IntMod<Modulus_>& right);
  template <uint32_t Modulus_>
  friend bool operator!=(const IntMod<Modulus_>& left,
                         const IntMod<Modulus_>& right);
  template <uint32_t Modulus_>
  friend bool operator<(const IntMod<Modulus_>& left,
                        const IntMod<Modulus_>& right);
  template <uint32_t Modulus_>
  friend bool operator<=(const IntMod<Modulus_>& left,
                         const IntMod<Modulus_>& right);
  template <uint32_t Modulus_>
  friend bool operator>(const IntMod<Modulus_>& left,
                        const IntMod<Modulus_>& right);
  template <uint32_t Modulus_>
  friend bool operator>=(const IntMod<Modulus_>& left,
                         const IntMod<Modulus_>& right);

 private:
  unsigned int value_m;

 public:
  IntMod() { value_m = 0; }
  IntMod(unsigned int value) { value_m = value % Modulus; }
  IntMod(unsigned long long value) { value_m = value % Modulus; }
  IntMod(int value) {
    int tmp = value % (int)Modulus;
    value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
  }
  IntMod(long long value) {
    int tmp = value % (long long)Modulus;
    value_m = tmp >= 0 ? tmp : Modulus - (unsigned int)(-tmp);
  }
  IntMod(const IntMod& other) : value_m(other.value_m) {}
  IntMod& operator=(const IntMod& other) {
    value_m = other.value_m;
    return *this;
  }
  const IntMod& operator+() const { return *this; }
  IntMod operator-() const { return IntMod(Modulus - value_m); }
  IntMod& operator++() {
    ++value_m;
    if (value_m == Modulus) value_m = 0;
    return *this;
  }
  IntMod& operator--() {
    if (value_m == 0) value_m = Modulus;
    --value_m;
    return *this;
  }
  IntMod operator++(int dummy) {
    IntMod tmp(*this);
    ++(*this);
    return tmp;
  }
  IntMod operator--(int dummy) {
    IntMod tmp(*this);
    --(*this);
    return tmp;
  }
  IntMod& operator+=(const IntMod& right) {
    value_m += right.value_m;
    if (value_m >= Modulus) value_m -= Modulus;
    return *this;
  }
  IntMod& operator-=(const IntMod& right) {
    if (value_m < right.value_m) value_m += Modulus;
    value_m -= right.value_m;
    return *this;
  }
  IntMod& operator*=(const IntMod& right) {
    value_m = ((unsigned long long)value_m * right.value_m) % Modulus;
    return *this;
  }
  IntMod& operator/=(const IntMod& right) {
    (*this) *= (right.Inverse());
    return *this;
  }
  IntMod operator[](unsigned long long exp) const { return Pow(exp); }
  IntMod Inverse() const { return (*this).Pow(Modulus - 2); }
  IntMod Pow(unsigned long long exp) const {
    IntMod product = 1;
    IntMod factor(*this);
    while (exp > 0) {
      if (exp & 1) product *= factor;
      factor *= factor;
      exp >>= 1;
    }
    return product;
  }
  unsigned int Get_value() const { return value_m; }
  static IntMod Fact(unsigned int num) {
    static std::vector<IntMod> table(1, 1);
    if (table.size() > num) return table[num];
    int old_size = table.size();
    table.resize(num + 1);
    for (int i = old_size; i <= num; i++) {
      table[i] = table[i - 1] * i;
    }
    return table[num];
  }
  static IntMod Combi(unsigned int n, unsigned int r) {
    if (n < r) throw "okashii";
    return IntMod::Fact(n) / (IntMod::Fact(n - r) * IntMod::Fact(r));
  }
  static std::vector<IntMod> Inverse_list(int size) {
    assert(size < Modulus);
    std::vector<IntMod> ret_arr(size + 1);
    ret_arr[1] = 1;
    for (int i = 2; i <= size; ++i) {
      ret_arr[i] = ((unsigned long long)(Modulus - Modulus / i) *
                    ret_arr[Modulus % i].Get_value()) %
                   Modulus;
    }
    return ret_arr;
  }
};
template <uint32_t Modulus>
IntMod<Modulus> operator+(const IntMod<Modulus>& left,
                          const IntMod<Modulus>& right) {
  IntMod<Modulus> ret(left);
  ret += right;
  return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator-(const IntMod<Modulus>& left,
                          const IntMod<Modulus>& right) {
  IntMod<Modulus> ret(left);
  ret -= right;
  return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator*(const IntMod<Modulus>& left,
                          const IntMod<Modulus>& right) {
  IntMod<Modulus> ret(left);
  ret *= right;
  return ret;
}
template <uint32_t Modulus>
IntMod<Modulus> operator/(const IntMod<Modulus>& left,
                          const IntMod<Modulus>& right) {
  IntMod<Modulus> ret(left);
  ret /= right;
  return ret;
}
template <uint32_t Modulus>
bool operator==(const IntMod<Modulus>& left, const IntMod<Modulus>& right) {
  return left.value_m == right.value_m;
}
template <uint32_t Modulus>
bool operator!=(const IntMod<Modulus>& left, const IntMod<Modulus>& right) {
  return left.value_m != right.value_m;
}
template <uint32_t Modulus>
bool operator<(const IntMod<Modulus>& left, const IntMod<Modulus>& right) {
  return left.value_m < right.value_m;
}
template <uint32_t Modulus>
bool operator<=(const IntMod<Modulus>& left, const IntMod<Modulus>& right) {
  return left.value_m <= right.value_m;
}
template <uint32_t Modulus>
bool operator>(const IntMod<Modulus>& left, const IntMod<Modulus>& right) {
  return left.value_m > right.value_m;
}
template <uint32_t Modulus>
bool operator>=(const IntMod<Modulus>& left, const IntMod<Modulus>& right) {
  return left.value_m >= right.value_m;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator+(const IntMod<Modulus>& left, Integer right) {
  return left + IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator+(Integer left, const IntMod<Modulus>& right) {
  return IntMod<Modulus>(left) + right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator-(const IntMod<Modulus>& left, Integer right) {
  return left - IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator-(Integer left, const IntMod<Modulus>& right) {
  return IntMod<Modulus>(left) - right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator*(const IntMod<Modulus>& left, Integer right) {
  return left * IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator*(Integer left, const IntMod<Modulus>& right) {
  return IntMod<Modulus>(left) * right;
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator/(const IntMod<Modulus>& left, Integer right) {
  return left / IntMod<Modulus>(right);
}
template <uint32_t Modulus, class Integer>
IntMod<Modulus> operator/(Integer left, const IntMod<Modulus>& right) {
  return IntMod<Modulus>(left) / right;
}
template <uint32_t Modulus>
std::istream& operator<<(std::istream& ist, const IntMod<Modulus>& val) {
  uint64_t tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
template <uint32_t Modulus>
std::ostream& operator<<(std::ostream& ost, const IntMod<Modulus>& val) {
  ost << val.Get_value();
  return ost;
}
IntMod<1000000007> operator"" _m(unsigned long long num) {
  return IntMod<1000000007>(num);
}
using namespace std;
const int INF = 0x3FFFFFFF;
const long long INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream& operator>>(istream& ist, pair<T, U>& right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream& operator<<(ostream& ost, const pair<T, U>& right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible& val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible& val) {
  for (int i = 0; i < M; ++i) Fill(dest[i], val);
}
template <class T>
T Compare(T left, T right) {
  return left > right ? 1 : (left < right ? -1 : 0);
}
istream& Ignore(istream& ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
template <class T>
T Next() {
  T buf;
  cin >> buf;
  return buf;
}
istream& operator>>(istream& ist, __int128& val) {
  long long tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream& operator<<(ostream& ost, __int128 val) {
  long long tmp = val;
  ost << tmp;
  return ost;
}
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
int N, K;
int A[300000];
int B[300000];
int C[300001];
bool ap[300001];
using MM = IntMod<998244353>;
void pre() {
  deque<int> Q;
  for (long long i = (0), i_max = (N + K - 1); i < i_max; ++i) {
    int p = A[i % N];
    while (!Q.empty() && A[Q.back() % N] <= p) {
      Q.pop_back();
    }
    Q.push_back(i);
    if (Q.front() == i - K) Q.pop_front();
    if (i >= K - 1) B[i - (K - 1)] = A[Q.front() % N];
  }
  for (long long i = (0), i_max = (N); i < i_max; ++i) {
    ++C[B[i]];
    ap[A[i]] = true;
  }
  for (long long i = (2), i_max = (N + 1); i < i_max; ++i) {
    C[i] += C[i - 1];
  }
}
int times() {
  int no = 0;
  while (no < N && A[no] == 1) {
    ++no;
  }
  if (no == N) return N;
  int st = no;
  while (st < N && A[st] != 1) {
    ++st;
  }
  if (st == N) return no;
  int di = 0;
  while (di < N && A[(st + di) % N] == 1) {
    ++di;
  }
  int cnt = 0;
  for (long long i = (0), i_max = (N); i < i_max; ++i) {
    if (A[i] == 1) ++cnt;
  }
  if (cnt != di) return -1;
  return di;
}
MM sub() {
  K = times();
  if (K == -1) return 0;
  pre();
  MM ans = 1;
  int cnt = 0;
  for (long long i = (1), i_max = (N + 1); i < i_max; ++i) {
    if (ap[i]) continue;
    ans *= C[i] - (i - 1);
  }
  return ans;
}
int main() {
  cin >> N;
  for (long long i = (0), i_max = (N); i < i_max; ++i) {
    cin >> A[i];
  }
  cout << sub() << endl;
  return 0;
}
