#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n, len, vis[800000], a[800000], ans, sum, num[800000], q[800000],
    cnt[800000];
void all_1() {
  for (long long i = 1; i <= n; ++i)
    if (a[i] != 1) return;
  puts("0");
  exit(0);
}
void no_1() {
  bool fl = 1;
  for (long long i = 1; i <= n; ++i)
    if (a[i] == 1) {
      fl = 0;
      break;
    }
  if (fl) {
    puts("0");
    exit(0);
  }
}
void too_long() {
  for (long long i = 2, j = 1; i <= n; ++i) {
    if (a[i] == a[i - 1])
      ++j;
    else
      j = 1;
    if (j > len) {
      puts("0");
      exit(0);
    }
  }
}
void not_continue() {
  for (long long i = 2; i <= n; ++i) {
    if (a[i] != a[i - 1] && vis[a[i]]) {
      puts("0");
      exit(0);
    }
    vis[a[i]] = 1;
  }
  memset(vis, 0, sizeof vis);
}
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; ++i) scanf("%lld", a + i);
  all_1();
  no_1();
  for (long long i = 1; i <= n; ++i) a[i + n] = a[i];
  for (long long i = 2; i <= n + 1; ++i)
    if (a[i] == 1 && a[i - 1] != 1) {
      for (long long j = 1; j <= n; ++j) a[j] = a[j + i - 1];
      break;
    }
  a[n + 1] = 0;
  for (len = 1; len <= n; ++len)
    if (a[len + 1] != 1) break;
  for (long long i = n, tt = 1, ww = 0; i; --i) {
    for (; tt <= ww && q[tt] >= i + len; ++tt)
      ;
    for (; tt <= ww && a[q[ww]] < a[i]; --ww)
      ;
    q[++ww] = i;
    ++cnt[a[q[tt]]];
  }
  too_long();
  not_continue();
  for (long long i = len + 1; i <= n; ++i) {
    if (a[i] > a[i - 1]) {
      long long x = a[i - 1];
      if (vis[x]) {
        puts("0");
        return 0;
      }
      vis[x] = i - len;
      num[x] = 1;
    }
    if (a[i] < a[i - 1]) {
      long long x = a[i];
      if (vis[x]) {
        puts("0");
        return 0;
      }
      vis[x] = i - len;
      num[x] = 1;
    }
  }
  for (long long i = 2; i <= n; ++i)
    if (a[i] > a[i - 1] && !vis[a[i]]) {
      vis[a[i]] = 1;
      for (long long j = i; a[j] == a[i]; ++j) ++num[a[i]];
      num[a[i]] = len - num[a[i]] + 1;
      for (long long j = i - 1; j >= i - num[a[i]] + 1; --j)
        if (a[j] > a[i]) {
          puts("0");
          return 0;
        }
    }
  ans = 1;
  for (long long i = 1; i <= n; ++i) {
    sum += cnt[i];
    if (vis[i])
      ans = ans * num[i] % mod;
    else
      ans = ans * sum % mod;
    --sum;
  }
  printf("%lld\n", ans);
  return 0;
}
