#include using namespace std; #include using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) {a = max(a, b);} template void chmin(T& a, const T& b) {a = min(a, b);} using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; constexpr int FACT_SIZE = 4010000; mint Fact[FACT_SIZE + 1]; mint iFact[FACT_SIZE + 1]; const auto fact_init = [] { Fact[0] = mint::raw(1); for(int i = 1; i <= FACT_SIZE; ++i) { Fact[i] = Fact[i-1] * i; } iFact[FACT_SIZE] = Fact[FACT_SIZE].inv(); for(int i = FACT_SIZE; i; --i) { iFact[i-1] = iFact[i] * i; } return false; }(); mint comb(int n, int k) { if (k == 0) return mint::raw(1); assert(n >= 0 && k >= 0); if (k > n) return mint::raw(0); return Fact[n] * iFact[n - k] * iFact[k]; } mint icomb(int n, int k) { return iFact[n] * Fact[n - k] * Fact[k]; } mint fact(int n) {return Fact[n];} mint perm(int n, int k) { assert(0 <= n); return Fact[n] * iFact[n - k]; } mint hook(const VI& a) { if (a.empty()) return 1; int n = a.size(); int tot = accumulate(all(a), 0); mint res = Fact[tot]; VI ends(*max_element(all(a)) + 1); for(int x: a) ends[x]++; mint v = 1; rep(i, n) { int now = n - i + a[i]; rep(j, a[i]) { now -= ends[j] + 1; v *= now; } } return res / v; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VI d; int pre = 0; rep(_, n) { int a; cin >> a; rrep(_, a - pre) d.emplace_back(1); d.emplace_back(0); pre = a; } int sz = d.size(); VI d0; VI d1; rep(b, 2) { for(int i = sz - 1 - b; i >= 0; i -= 2) d0.emplace_back(d[i]); swap(d0, d1); } if (abs(2 * accumulate(all(d0), 0) - (1 + 2 * accumulate(all(d1), 0))) > 1) { cout << 0 << '\n'; return 0; } mint ans = 1; int cnt0 = 0, cnt1 = 0; rep(_, 2) { VI p; int now = 0; for(int x: d0) { if (x == 0) now++; else p.emplace_back(now); } reverse(all(p)); ans *= hook(p); cnt0 = accumulate(all(p), 0); swap(cnt0, cnt1); swap(d0, d1); } ans *= comb(cnt0 + cnt1, cnt0); cout << ans.val() << '\n'; }