Problem:
Score : 300 points

Problem Statement

Given is an integer sequence D_1,...,D_N of N elements. Find the number, modulo 998244353, of trees with N vertices numbered 1 to N that satisfy the following condition:

For every integer i from 1 to N, the distance between Vertex 1 and Vertex i is D_i.

Notes

A tree of N vertices is a connected undirected graph with N vertices and N-1 edges, and the distance between two vertices are the number of edges in the shortest path between them.

Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.

Constraints

1 \leq N \leq 10^5

0 \leq D_i \leq N-1

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N

Output

Print the answer.

Sample Input 1

4
0 1 1 2

Sample Output 1

2

For example, a tree with edges (1,2), (1,3), and (2,4) satisfies the condition.

Sample Input 2

4
1 1 1 1

Sample Output 2

0

Sample Input 3

7
0 3 2 1 2 2 1

Sample Output 3

24

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#ifndef DEBUG
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>

//#define DEBUG
#ifdef DEBUG
#define Debug(...) cerr << "# Debug:" _ __PRETTY_FUNCTION__ _ '-' _ __LINE__ _ '(' _ #__VA_ARGS__ _ (__VA_ARGS__) _ ')' << endl;
#define FAST ((void)0);
#else
#define endl '\n'
#define Debug(x) ((void)0);
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#endif // DEBUG
#define _ << ' ' <<
#define Fr(i, s, e) for(auto i = s; i < e; ++i)
#define CASE int _T;cin >> _T;Fr(tt, 1, _T+1)
#define Eb emplace_back
#define Iter(v) v.begin(),v.end()
#define Lsort(v) sort(Iter(v))
#define Sz(v) ll(v.size())
#define Ft first
#define Sd second

using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;

constexpr ld PI = acos(-1), eps = 1e-9;
constexpr ll MOD97 = 1e9+7;
constexpr ll MAX = 1e5+5;
constexpr ll INF = 1023456789;
constexpr ll IINF = 1234567890123456789LL;

inline ll getint() {
    ll x=0,s=1;char c=getchar();
    while((c<'0'||c>'9')&&c!='-')c=getchar();
    if(c=='-')c=getchar(),s=-1;
    while('0'<=c&&c<='9')x=x*10+c-'0',c=getchar();
    return x*s;
}

inline ll fpow(ll a, ll b, ll m = MOD97) {
    if (b == 0) return 1%m;
    if (b == 1) return a%m;
    ll r = fpow((a*a)%m, b/2, m);
    return (r*(b&1?a:1))%m;
}

signed main() {
    FAST
    srand(time(NULL));

    constexpr ll MOD = 998244353;
    int n;
    cin >> n;
    vector<int> D(n);
    Fr(i, 0, n) cin >> D[i];
    vector<int> cnt(n);
    Fr(i, 0, n) cnt[D[i]]++;
    int mx = n;
    bool can = cnt[0]==1;
    if (can) {
        while (cnt[mx-1] == 0) mx--;
        Fr(i, 0, mx) can = can && cnt[i];
    }
    if (!can)
        cout << "0\n";
    else {
        ll ans = 1;
        Fr(i, 1, mx)
            ans = (ans*fpow(cnt[i-1], cnt[i], MOD))%MOD;
        cout << ans << endl;
    }

    return 0;
}