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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io;
use std::collections::HashMap;
//use std::collections::BinaryHeap;
//use std::collections::HashSet;
//use std::mem; // mem::swap(&mut x,&mut y);
use std::cmp; // cmp::max,cmp::min

// Rust memo: https://hackmd.io/i0lQY1OKTDW1t66TECOmiw?both

#[allow(dead_code)]
fn read_line() -> String{
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn read_charvec() -> Vec<char>{
    read_line().chars().collect()
}

#[allow(dead_code)]
fn read_ints() -> Vec<i64>{
    let s = read_line();
    let split:Vec<&str> = s.split(" ").collect();
    split.iter().map(|&x| x.to_string().parse().unwrap()).collect()
}

#[allow(dead_code)]
fn read_int() -> i64{
    read_ints()[0]
}

#[allow(dead_code)]
fn reverse(s: &String) -> String{
    s.chars().rev().collect::<String>()
}

#[allow(dead_code)]
fn read_lines(n:i64) -> Vec<String>{
    let mut xs = Vec::new();

    for _i in 0..n{
        xs.push(read_line());
    }

    xs
}

#[allow(dead_code)]
fn read_ints2(n:i64) -> Vec<i64>{
    read_lines(n).iter().map(|x| x.parse().unwrap()).collect()
}

#[allow(dead_code)]
fn arange(start:i64,end:i64,step:i64) -> Vec<i64>{
    let mut ret = Vec::new();
    let mut i = start;

    while i < end{
        ret.push(i);
        i += step;
    }

    ret
}

#[allow(dead_code)]
fn mypow(n:i64, mut x:i64, n_mod : i64)->i64{
    // n**x
    let mut temp = n;
    let mut result = 1;

    while x != 0{
        if x & 1 != 0{
            result *= temp;
            result %= n_mod;
        }
        x /= 2;
        temp *= temp;
        temp %= n_mod;
    }

    result
}

#[allow(dead_code)]
const DXS : [i64; 4] = [0,0,1,-1];
#[allow(dead_code)]
const DYS : [i64; 4]  = [1,-1,0,0];
#[allow(dead_code)]
const INF : i64 = 999999999;
#[allow(dead_code)]
const N_PRIME : i64 = 1000000007;

////////////////////////////////////////////////

fn f(xs:&Vec<i64>)->(HashMap<i64, usize>, usize){
    let mut r = HashMap::new();
    let mut max_v = 0;

    for x in xs.iter(){
        if r.contains_key(x){
            let v = r[x];
            r.insert(*x, v+1);
        }else{
            r.insert(*x, 1);
        }
        max_v = cmp::max(max_v, *x as usize);
    }

    (r, max_v)
}

const N_DIV : i64 = 998244353;

fn abort(){
    println!("0");
    std::process::exit(0);
}

fn main(){
    let n = read_int();
    let xs = read_ints();

    if xs[0] != 0{
        abort();
    }

    let (hm, m )= f(&xs);

    if hm[&0] != 1{
        abort();
    }

    let mut n = 1;
    for i in 1..m+1{
        if hm.contains_key(&(i as i64)) == false{
            abort();
        }

        let last_n = hm[&((i - 1)as i64)];
        let now_n = hm[&(i as i64)];

        n *= mypow(last_n as i64, now_n as i64, N_DIV);
    }
    println!("{}",n);
}