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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 998244353;

vector<long long int> inverse;
vector<long long int> f, f_inverse;

void factorial_Initialize(int max){
    vector<long long int> a(max+1,1);
    inverse = a;
    f = a;
    f_inverse = a;

    for (int i=2; i<=max; i++){
        inverse[i]   = MOD - inverse[MOD % i] * (MOD / i) % MOD;
        f[i]         = f[i-1]         * i          % MOD;
        f_inverse[i] = f_inverse[i-1] * inverse[i] % MOD;
    }
}

long long int nCr(int n, int r){
    if(n < r || r < 0) return 0;
    if (n >= f.size()){
        factorial_Initialize(n);
    }
    return f[n] * f_inverse[r] % MOD * f_inverse[n-r] % MOD;
}

long long int modpow(long long int b, long long int p){
    long long int num = 1, power = b;
    for(int i=0; i<=30; i++){
        if((p>>i) & 1){
            num *= power;
            num %= MOD;
        }

        power *= power;
        power %= MOD;
    }
    return num;
}

int main(){
    int n;
    cin >> n;

    vector<pair<int,int>> d(n);
    vector<int> depth(n);
    for(int i=0; i<n; i++){
        cin >> d[i].first;
        d[i].second = i;
        depth[d[i].first] += 1;
    }
    sort(d.begin(),d.end());
    //for(int i=0; i<n; i++) cout << d[i].first << " " << d[i].second << endl;

    if(d[0].first != 0 || d[0].second != 0){
        cout << 0 << endl;
        return 0;
    }

    bool flag = true;
    long long int ans = 1;
    for(int i=1; i<n; i++){
        if(flag == false && depth[i] != 0){
            cout << 0 << endl;
            return 0;
        }
        ans *= modpow(depth[i-1], depth[i]);
        ans %= MOD;
        if(depth[i] == 0) flag = false;
    }
    cout << ans << endl;
    return 0;
}