#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const int MOD = 998244353;
int n, A[N], a[N];
bool vis[N];
int L[N], R[N];
int s[N];
int tim;
int ans = 1;
struct qq {
  int len, id;
} h[N];
int cnt = 0;
int pos;
bool cmp(qq x, qq y) { return x.id < y.id; }
int mul(int x, int y) { return (long long)x * y % MOD; }
void solve() {
  for (int u = 1; u <= n; u++)
    if (s[u] != 0) {
      pos = u;
      break;
    }
  int now = pos, lst = s[now], o = 0;
  do {
    now++;
    if (now > n) now = 1;
    if (s[now] != 0) {
      if (o != 0) {
        cnt++;
        h[cnt].len = o;
        h[cnt].id = max(lst, s[now]);
      }
      lst = s[now];
      o = 0;
    } else
      o++;
  } while (now != pos);
  sort(h + 1, h + 1 + cnt, cmp);
  int lalal = 0;
  now = 0;
  for (int u = 1; u <= n; u++)
    if (vis[u] == false) {
      while (now <= cnt && h[now + 1].id < u) {
        now++;
        lalal = lalal + h[now].len;
      }
      if (lalal == 0) {
        printf("0\n");
        return;
      }
      ans = mul(ans, lalal);
      lalal--;
    }
  printf("%d\n", ans);
}
int main() {
  memset(s, 0, sizeof(s));
  memset(vis, false, sizeof(vis));
  scanf("%d", &n);
  pos = -1;
  for (int u = 0; u < n; u++) {
    scanf("%d", &A[u]);
    if (pos == -1 && A[u] == 1) pos = u;
  }
  if (pos == -1) {
    printf("0\n");
    return 0;
  }
  if (pos == 0) {
    for (int u = n - 1; u >= 0; u--)
      if (A[u] == 1)
        pos = u;
      else
        break;
  }
  for (int u = 1; u <= n; u++) a[u] = A[(pos + u - 1) % n];
  for (int u = 1; u <= n; u++) {
    tim = tim + (a[u] == 1);
    if (a[u] != a[u - 1]) {
      if (vis[a[u]] == true) {
        printf("0\n");
        return 0;
      }
      vis[a[u]] = true;
    }
  }
  for (int u = 1; u <= n; u++)
    if (a[u] == a[u - 1])
      L[u] = L[u - 1];
    else
      L[u] = u;
  for (int u = n; u >= 1; u--)
    if (a[u] == a[u + 1])
      R[u] = R[u + 1];
    else
      R[u] = u;
  for (int u = 1; u <= n; u++)
    if (u == L[u]) {
      if (R[u] - L[u] + 1 > tim) {
        printf("0\n");
        return 0;
      }
      int pos = -1, pos1 = -1;
      int l, r;
      l = L[u] - 1;
      if (l <= 0) l += n;
      r = R[u] + 1;
      if (r > n) r -= n;
      if (a[l] > a[u]) pos = L[u] + tim - 1;
      if (pos > n) pos = pos - n;
      if (a[r] > a[u]) pos1 = R[u];
      if (pos != -1 && pos != pos1) {
        printf("0\n");
        return 0;
      }
      if (pos != -1) {
        if (s[pos] != 0) {
          printf("0\n");
          return 0;
        }
        s[pos] = a[u];
      } else if (pos1 != -1) {
        if (s[pos1] != 0) {
          printf("0\n");
          return 0;
        }
        s[pos1] = a[u];
      }
      if (pos == -1 && pos1 == -1) {
        if (tim < (R[u] - L[u] + 1)) {
          printf("0\n");
          return 0;
        }
        if (L[u] == 1 && R[u] == n)
          ans = mul(ans, n);
        else {
          int t = min(tim - (R[u] - L[u] + 1) + 1, (R[r] - L[r] + 1));
          ans = mul(ans, t);
        }
        s[R[u]] = a[u];
      }
    }
  solve();
  return 0;
}
