#include #include #include using namespace std; using namespace __gnu_pbds; #define ar array #define vt vector #define pq priority_queue #define pu push #define pub push_back #define em emplace #define emb emplace_back #define mt make_tuple #define all(x) x.begin(), x.end() #define allr(x) x.rbegin(), x.rend() #define allp(x, l, r) x.begin() + l, x.begin() + r #define len(x) (int)x.size() #define uniq(x) unique(all(x)), x.end() using ll = long long; using ld = long double; using ull = unsigned long long; template void re(array & x); template void re(vt & x); template void re(T& x) { cin >> x; } template void re(T& x, M&... args) { re(x), re(args...); } template void re(vt & x) { for(auto& it : x) re(it); } template void re(array & x) { for(auto& it : x) re(it); } template void wr(const array & x); template void wr(const vt & x); template void wr(const T& x) { cout << x; } template void wr(const T& x, const M&... args) { wr(x), wr(args...); } template void wr(const vt & x) { for(auto it : x) wr(it, ' '); } template void wr(const array & x) { for(auto it : x) wr(it, ' '); } template auto mvt(size_t n, M&&... args) { if constexpr(sizeof...(args) == 1) return vector(n, args...); else return vector(n, mvt(args...)); } void set_fixed(int p = 0) { cout << fixed << setprecision(p); } void set_scientific() { cout << scientific; } void Open(const string& name) { #ifndef ONLINE_JUDGE (void)!freopen((name + ".in").c_str(), "r", stdin); (void)!freopen((name + ".out").c_str(), "w", stdout); #endif } const int mod = 998'244'353; template struct modint { using M = modint; unsigned x; modint(ll x = 0) { fix(x % MOD + MOD); } inline M& fix(unsigned _x) { x = (_x < MOD)? _x : _x - MOD; return *this; } explicit operator bool() const { return x != 0; } explicit operator long long() const { return x; } inline bool operator == (const M& other) const { return x == other.x; } inline bool operator != (const M& other) const { return x != other.x; } inline bool operator < (const M& other) const { return x < other.x; } inline bool operator > (const M& other) const { return x > other.x; } inline bool operator <= (const M& other) const { return x <= other.x; } inline bool operator >= (const M& other) const { return x >= other.x; } inline M operator + () const { return M().fix(x); } inline M operator - () const { return M().fix(MOD - x); } inline M& operator ++ () { this-> fix(x + 1); return *this; } inline M operator ++ (int) { M tmp(*this); ++(*this); return tmp; } inline M& operator -- () { this-> fix(x - 1 + MOD); return *this; } inline M operator -- (int) { M tmp(*this); --(*this); return tmp; } inline M operator + (const M& other) const { return M().fix(x + other.x); } inline M operator - (const M& other) const { return M().fix(x + MOD - other.x); } inline M operator * (const M& other) const { return M().fix((ull)x * other.x % MOD); } inline M operator / (const M& other) const { return *this * other.inv(); } inline M& operator += (const M& other) { return *this = *this + other; } inline M& operator -= (const M& other) { return *this = *this - other; } inline M& operator *= (const M& other) { return *this = *this * other; } inline M& operator /= (const M& other) { return *this = *this / other; } M lgput(ll b) const { M a = *this, res = 1; while (b) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } M inv() const { return lgput(MOD - 2); } friend M operator + (const ll& value, const M& n) { return n + M(value); } friend M operator + (const int& value, const M& n) { return n + M(value); } friend M operator - (const ll& value, const M& n) { return n - M(value); } friend M operator - (const int& value, const M& n) { return n - M(value); } friend M operator * (const ll& value, const M& n) { return n * M(value); } friend M operator * (const int& value, const M& n) { return n * M(value); } friend istream& operator >> (istream& in, M& n) { ll x; in >> x; n = x; return in; } friend ostream& operator << (ostream& os, const M& n) { return os << n.x; } }; using mint = modint; const int N = 1e6 + 1; mint fact[N], ifact[N]; void prec() { fact[0] = ifact[0] = 1; fact[1] = ifact[1] = 1; for (int i = 2; i < N; ++i) fact[i] = fact[i - 1] * i; ifact[N - 1] = fact[N - 1].inv(); for (int i = N - 2; i >= 2; --i) ifact[i] = ifact[i + 1] * (i + 1); } mint nCr(int n, int k) { if (n < 0 || k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } mint hookLength(vt hook) { if (hook.empty()) return mint(1); sort(all(hook)); std::vt hook_t(hook.back()); int sum = 0; mint num(1), den(1); for (int l : hook) { for (int j = 0; j < l; ++j) { num *= ++sum; den *= (++hook_t.at(j)) + l - 1 - j; } } return num / den; } void solve() { int n; re(n); vt a(n); re(a); a.insert(a.begin(), 0); string s; for (int i = 1; i <= n; ++i) s += string(a[i] - a[i - 1], '1') + '0'; int s0 = 0, s1 = 0; vt even, odd; for (int i = 0, cnt = 0; i < len(s); i += 2) if (s[i] == '0') s0 += cnt, even.pub(cnt); else ++cnt; for (int i = 1, cnt = 0; i < len(s); i += 2) if (s[i] == '0') s1 += cnt, odd.pub(cnt); else ++cnt; if (2 * (s0 + s1) != accumulate(all(a), 0)) return wr("0\n"); mint res = 1; res = nCr(s0 + s1, s0) * hookLength(even) * hookLength(odd); wr(res); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //Open(""); prec(); int t = 1; for(;t;t--) { solve(); } return 0; }