#include using namespace std; using ll = long long; constexpr int N = 200000 + 1, P = 998244353; int pre[N], nxt[N], a[N], p[N], stk[N], l[N], r[N], fa[N], fac[N], ifac[N], ord[N], sz[N]; bitset vis; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int top = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; pre[i] = i - 1; nxt[i] = i + 1; if (a[i] == 0) { stk[++top] = i; } } nxt[n] = 0; for (int i = 1; i <= n; ++i) { while (top && vis[stk[top]]) { --top; } if (!top) { cout << "0\n"; return 0; } int x = stk[top--]; p[x] = n + 1 - i; a[x] = 1 << 30; int l = pre[x], r = nxt[x]; if (l) { if (--a[l] < 0) { cout << "0\n"; return 0; } if (!a[l]) { stk[++top] = l; } nxt[l] = r; } if (r) { if (--a[r] < 0) { cout << "0\n"; return 0; } if (a[r] == 0) { stk[++top] = r; } pre[r] = l; } } top = 0; for (int i = 1; i <= n; ++i) { if (p[i] < p[stk[top]]) { --top; while (top && p[i] < p[stk[top]]) { --top; } l[i] = stk[top + 1]; fa[stk[top + 1]] = i; } r[stk[top]] = i; fa[i] = stk[top]; stk[++top] = i; } auto qPow = [](int a, int b) { int res = 1; for ( ; b; b >>= 1) { if (b & 1) { res = (ll)res * a % P; } a = (ll)a * a % P; } return res; }; fac[0] = 1; for (int i = 1; i <= n; ++i) { fac[i] = (ll)fac[i - 1] * i % P; } ifac[n] = qPow(fac[n], P - 2); for (int i = n; i; --i) { ifac[i - 1] = ifac[i] * (ll)i % P; } auto inv = [](int x) { return fac[x - 1] * (ll)ifac[x] % P; }; int rt = 1; while (fa[rt]) { ++rt; } stk[top = 1] = rt; int idx = 0; while (top) { int u = stk[top--]; ord[++idx] = u; if (l[u]) { stk[++top] = l[u]; } if (r[u]) { stk[++top] = r[u]; } } int ans = fac[n]; for (int i = n; i; --i) { int u = ord[i]; sz[u] = sz[l[u]] + sz[r[u]] + 1; ans = (ll)ans * inv(sz[u]) % P; } cout << ans << '\n'; return 0; }