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)

#include<bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
 
#define ll long long int
#define ld long double
#define bigint int64_t
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define inf 1e15
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;read(t);while(t--)
#define all(v) v.begin(),v.end()
#define pll pair<ll,ll>
#define inf 1e15
#define neg_inf -100000000000000LL
 
/*#include <ext/pb_ds/assoc_container.hpp> // Policy_based_Data_Structure 
#include <ext/pb_ds/graph_policy.hpp> 
using namespace __gnu_pbds;*/ 
 
#define forn(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i))
#define for1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i))
#define forr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i))
#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))
 
using namespace std;
template <typename T>
void read(T &x){
    cin >> x;
}
template <typename T , typename T0>
void read(T &x,T0 &y){
    cin >> x >> y;
}
 
template <typename T , typename T0 , typename T1>
void read(T &x,T0 &y,T1 &z){
    cin >> x >> y >> z;
}
 
template <typename T , typename T0 , typename T1 , typename T2>
void read(T &x,T0 &y,T1 &z,T2 &w){
    cin >> x >> y >> z >> w;
}
  
template <typename T>
void read(vector< T > &oneD,ll n){
    forn(i,n)
    {
        ll x;
        read(x);
        oneD.push_back(x);
    }
}
 
template <typename T>
void read(T oneD[] , ll n){
    for(ll i=0;i<n;i++){
        read(oneD[i]);
    }
}
 
template <typename T>
void write(vector< T > &oneD,ll n){
    for(ll i=0;i<n;i++){
        cout<<oneD[i]<<" ";
    }
    cout << endl;
}
 
template <typename T>
void write(T oneD[] ,int n){
    for(ll i=0;i<n;i++){
        write(oneD[i]);
    }
    cout << endl;
}
 
ll extended_GCD(ll a , ll b , ll &x , ll &y){
    if(a == 0){
        x = 0;
        y = 1;
        return b;
    }
    ll x1 , y1;
    ll gcd = extended_GCD(b%a , a , x1 , y1);
    x = y1 - (b/a)*x1; 
    y = x1;
    return gcd;
}
 
ll modinv(ll a , ll mod = MOD){
    ll x , y;
    extended_GCD(a , mod , x , y);
    if(x < 0) x += mod;
    return x;
}
 
//Chinese Remainder Theoram
ll find_x_CRT(ll num[],ll rem[],ll n)
{
    ll pro=1;
    forn(i,n)
    pro*=num[i];
    ll res=0;
    forn(i,n)
    {
        ll pp = pro/num[i];
        res+=(rem[i]*modinv(pp,num[i])*pp);
    }
    res%=pro;
    return res;
}
 
ll power(ll a, ll b, ll m = MOD) {
    a%=m;
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a %m;
        a = a * a %m;
        b >>= 1;
    }
    return res;
}
 
ll powe(ll a, ll b, ll m = MOD) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
 
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
string getString(char x) 
{ 
    string s(1, x); 
    return s;    
}

void add_edge(ll u,ll v,ll w,vector<vpll> &ad)
{
    ad[u].pb({v,w});
    ad[v].pb({u,w});
}

int main()
{
    flash;
    ll n;
    read(n);
    ll a[n];
    read(a,n);
    map<ll,ll> m={};
    forn(i,n)
    {
        m[a[i]]++;
    }
    ll mod =  998244353;
    ll zero = 0;
    forn(i,n)
    {
        if(a[i]==0)
        zero++;
    }
    if(zero==1)
    {
        ll ans = 1,pre=-1;
        for(auto x:m)
        {
            if(pre!=(-1))
            {
                if(abs(x.first-pre)>1)
                {
                    cout<<"0";
                    return 0;
                }
            }
            pre=x.first;            
        }
        forab(i,2,(n-1),1)
        {
            if(m[i-1]!=0&&m[i]!=0)
            {
            ll y = power(m[i-1],m[i],mod);
            ans = (ans%mod*y%mod)%mod;
            }
        }
        cout<<ans;
    }
    else
    {
        cout<<"0";
    }
    return 0;
}