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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <random>
#include <chrono>
#include <iomanip>
#define lint long long int
#define FOR(x, to) for(int x=0; x<(int)(to); x++) // for簡略表記
#define DUMP(ar) for(int i=0; i<(int)ar.size(); i++) cout << "[" << i << "]:" << ar[i] << endl // 配列一覧表示
#define DUMPL(ar) for(int i=0; i<(int)ar.size(); i++) { cout << ar[i]; if(i<(int)ar.size()-1) cout << ' '; } cout << endl; // 配列一行一覧表示
#define COUT(x) cout << x << endl // 単要素出力
#define ALL(ar) ar.begin(), ar.end() // コンテナの全要素
#define LOOPD(i) cout << "ループ[" << i << "]回目---------------" << endl // ループ回数デバグ
using namespace std;
// cout << fixed << setprecision(4) で小数点以下4桁の桁そろえ
// cout << setw(5) << ... で表示形式を5桁に設定 5桁未満なら頭に空白がつく 5桁越なら先頭から5桁まで
// cout << setw(5) << setfill('0') << ... で空白部分の埋め文字に5を使う
// getline(cin, [変数]); で空白などを含む入力の受け取り(行ごとの受け取り)
// cin >> ws; でそこまでにストリームに残されたホワイトスペースを取り除く(=以降のgetlineの有効化)

int comb(int n, int m){
    lint tmp = 1, tmp2 = 1;
    for(int i=0; i<m; i++){
        tmp *= n - i;
    }
    for(int i=1; i<=m; i++){
        tmp2 *= i;
    }

    return tmp / tmp2;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> d(n);
    FOR(i, n) cin >> d[i];
    
    sort(ALL(d));
    if(d[0] != 0){
        COUT(0);
        return 0;
    }

    // DUMPL(d);
    vector<int> cnt(1);
    lint ans = 1;
    int now_num = 0;
    lint p;
    for(int i=0; i<n; i++){
        if(now_num == d[i]){
            cnt[now_num]++;
        }else{
            now_num++;
            cnt.push_back(1);
        }
    }
    if(cnt[0] >= 2){
        COUT(0);
        return 0;
    }
    /*
    if(d[i] != now_num+1){
        COUT(0);
        return 0;
    }else
    */
    
    for(int i=1; i<(int)cnt.size(); i++){
        p = 1;
        for(int k=0; k<(int)cnt[i]; k++){
            p = (p*cnt[i-1]) % 998244353ll;
        }
        // LOOPD(i);
        // cout << "p:" << p << " ans:" << ans << endl;
        ans = (ans*p) % 998244353ll;
    }

    // ans = 998244358ll;
    COUT(ans);

    return 0;
}