#include <bits/stdc++.h>
template <class T>
using vv = std::vector<std::vector<T>>;
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& v) {
  int len = v.size();
  s << "\n";
  for (int(i) = (0); (i) < (len); ++(i)) {
    s << v[i];
    if (i < len - 1) {
      s << "\t";
    }
  }
  s << "\n";
  return s;
}
template <typename T>
std::ostream& operator<<(std::ostream& s,
                         const std::vector<std::vector<T>>& vv) {
  int len = vv.size();
  for (int(i) = (0); (i) < (len); ++(i)) {
    s << vv[i];
  }
  return s;
}
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::deque<T>& v) {
  int len = v.size();
  s << "\n";
  for (int(i) = (0); (i) < (len); ++(i)) {
    s << v[i];
    if (i < len - 1) {
      s << "\t";
    }
  }
  s << "\n";
  return s;
}
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::deque<std::deque<T>>& vv) {
  int len = vv.size();
  for (int(i) = (0); (i) < (len); ++(i)) {
    s << vv[i];
  }
  return s;
}
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::set<T>& v) {
  s << "\n";
  for (auto elm : v) {
    s << elm << "\t";
  }
  s << "\n";
  return s;
}
inline void scan(int& a) { scanf("%d", &a); }
inline void scan(int& a, int& b) { scanf("%d%d", &a, &b); }
inline void scan(int& a, int& b, int& c) { scanf("%d%d%d", &a, &b, &c); }
inline void scan(int& a, int& b, int& c, int& d) {
  scanf("%d%d%d%d", &a, &b, &c, &d);
}
inline void scan(std::vector<int>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void scan(std::vector<std::vector<int>>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void scan(long long& a) { scanf("%lld", &a); }
inline void scan(long long& a, long long& b) { scanf("%lld%lld", &a, &b); }
inline void scan(long long& a, long long& b, long long& c) {
  scanf("%lld%lld%lld", &a, &b, &c);
}
inline void scan(long long& a, long long& b, long long& c, long long& d) {
  scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
}
inline void scan(std::vector<long long>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void scan(std::vector<std::vector<long long>>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void scan(char& a) { scanf(" %c", &a); }
inline void scan(std::vector<char>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void scan(std::vector<std::vector<char>>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void scan(std::string& s) {
  char BUF[3000000];
  scanf(" %s", BUF);
  s = std::string(BUF);
}
inline void scan(std::deque<int>& v) {
  int sz = v.size();
  for (int(i) = (0); (i) < (sz); ++(i)) {
    scan(v[i]);
  }
}
inline void print(int a) { printf("%d\n", a); }
inline void print(int a, int b) { printf("%d %d\n", a, b); }
inline void print(long long a) { printf("%lld\n", a); }
inline void print(long long a, long long b) { printf("%lld %lld\n", a, b); }
inline void print(std::string s) { std::cout << s << "\n"; }
inline void print_yn(bool flg) {
  if (flg) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
}
using namespace std;
int mod = 998244353;
int main() {
  int n;
  scan(n);
  deque<int> a(n);
  scan(a);
  while (true) {
    if (a[0] == a[n - 1]) {
      a.push_front(a[n - 1]);
      a.pop_back();
    } else {
      break;
    }
  }
  int cur = -1;
  map<int, std::vector<int>> mp;
  for (int(i) = (0); (i) < (n); ++(i)) {
    if (cur != a[i]) {
      if (mp.find(cur) != end(mp)) {
        mp[cur][1] = i;
      }
      cur = a[i];
      if (mp.find(cur) != end(mp)) {
        printf("0\n");
        return 0;
      }
      mp[cur] = {i, 0};
    }
  }
  mp[cur][1] = n;
  deque<deque<int>> elms;
  for (int(i) = (0); (i) < (n); ++(i)) {
    int sz = elms.size();
    if (sz == 0 || elms[sz - 1][0] != a[i]) {
      elms.push_back({a[i], 0, 0});
    }
  }
  int m = elms.size();
  for (int(i) = (0); (i) < (m); ++(i)) {
    std::vector<int> tmpv = mp[elms[i][0]];
    elms[i][1] = tmpv[0];
    elms[i][2] = tmpv[1];
  }
  map<int, int> index;
  for (int(i) = (0); (i) < (m); ++(i)) {
    index[elms[i][0]] = i + 1;
  }
  elms.push_back({elms[0][0], elms[0][1] + n, elms[0][2] + n});
  elms.push_front({elms[m - 1][0], elms[m - 1][1] - n, elms[m - 1][2] - n});
  if (index.find(1) == end(index)) {
    printf("0\n");
    return 0;
  }
  int k = elms[index[1]][2] - elms[index[1]][1];
  long long ans = 1;
  int sum_space = 0;
  std::vector<int> spaces(m, 0);
  for (int(i) = (0); (i) < (m); ++(i)) {
    spaces[i] = elms[i][2] - elms[i][1] + 1;
  }
  for (int(i) = (1); (i) < (n + 1); ++(i)) {
    if (index.find(i) == end(index)) {
      if (sum_space == 0) {
        printf("0\n");
        return 0;
      }
      ans = ans * sum_space % mod;
      sum_space -= 1;
      continue;
    }
    int ind = index[i];
    int k_ = elms[ind][2] - elms[ind][1];
    if (k_ > k) {
      printf("0\n");
      return 0;
    }
    int prev_ind = ind - 1;
    int next_ind = ind + 1;
    if (elms[prev_ind][0] > i && i < elms[next_ind][0]) {
      if (k_ == k) {
      } else {
        printf("0\n");
        return 0;
      }
    } else if (elms[prev_ind][0] > i && i > elms[next_ind][0]) {
      sum_space += (elms[next_ind][1] - elms[ind][1]);
    } else if (elms[prev_ind][0] < i && i < elms[next_ind][0]) {
      sum_space += (elms[next_ind][1] - k - elms[prev_ind][1] - 1);
    } else if (elms[prev_ind][0] < i && i > elms[next_ind][0]) {
      ans = ans * (elms[ind][1] - (elms[next_ind][1] - k) + 1) % mod;
      sum_space += (elms[next_ind][1] - elms[prev_ind][1] - 2);
    }
  }
  print(ans);
  return 0;
}
