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.42.0) (judge verdict: Wrong Answer)

// -*- coding:utf-8-unix -*-
use std::cmp;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::LinkedList;
use std::collections::VecDeque;
use std::fs::File;
use std::io;
use std::io::prelude::*;

const INF: i64 = 1223372036854775807;
const MOD: i64 =  998244353;

use std::io::stdin;
use std::io::stdout;
use std::io::Write;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

#[allow(dead_code)]
pub fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}
#[allow(dead_code)]
pub fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}
#[allow(dead_code)]
/// (gcd, x, y)
pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (gcd, x, y) = extgcd(b, a % b);
        (gcd, y, x - (a / b) * y)
    }
}
#[allow(dead_code)]
/// x ^ n % m
pub fn mod_pow(x: u64, n: u64, m: u64) -> u64 {
    let mut res = 1;
    let mut x = x % m;
    let mut n = n;
    while n > 0 {
        if n & 1 == 1 {
            res = (res * x) % m;
        }
        x = (x * x) % m;
        n >>= 1;
    }
    res
}
#[allow(dead_code)]
pub fn mod_inverse(a: u64, m: u64) -> u64 {
    let (_, x, _) = extgcd(a as i64, m as i64);
    ((m as i64 + x) as u64 % m) % m
}
#[allow(dead_code)]
pub fn fact_table(len: usize, m: u64) -> Vec<u64> {
    let mut res = vec![1; len + 1];
    for i in 1..len + 1 {
        res[i] = (i as u64 * res[i - 1]) % m;
    }
    res
}
#[allow(dead_code)]
/// Factorial and Inverse factorial table
pub fn fact_inv_table(size: usize, m: u64) -> (Vec<u64>, Vec<u64>) {
    let mut fact = vec![1; size];
    let mut fact_inv = vec![1; size];
    for i in 2..size {
        fact[i] = fact[i - 1] * i as u64 % m;
        fact_inv[i] = m - ((m / i as u64) * fact_inv[(m % i as u64) as usize] % m);
    }
    for i in 1..size {
        fact_inv[i] = fact_inv[i - 1] * fact_inv[i] % m;
    }
    (fact, fact_inv)
}
#[allow(dead_code)]
/// (a mod p, e when n! = a p\^e)
pub fn mod_fact(n: u64, p: u64, fact: &[u64]) -> (u64, u64) {
    if n == 0 {
        (1, 0)
    } else {
        let (a, b) = mod_fact(n / p, p, fact);
        let pow = b + n / p;
        if n / p % 2 != 0 {
            (a * (p - fact[(n % p) as usize]) % p, pow)
        } else {
            (a * fact[(n % p) as usize] % p, pow)
        }
    }
}
#[allow(dead_code)]
/// C(n, k) % p
pub fn mod_comb(n: u64, k: u64, p: u64, fact: &[u64]) -> u64 {
    if n < k {
        0
    } else {
        let (a1, e1) = mod_fact(n, p, fact);
        let (a2, e2) = mod_fact(k, p, fact);
        let (a3, e3) = mod_fact(n - k, p, fact);
        if e1 > e2 + e3 {
            0
        } else {
            a1 * mod_inverse(a2 * a3 % p, p) % p
        }
    }
}
#[allow(dead_code)]
/// H(n, k) % p
pub fn mod_comb_repetition(n: u64, k: u64, p: u64, fact: &[u64]) -> u64 {
    mod_comb(n - 1 + k, n - 1, p, fact)
}



fn solve() {
    let n :i64 = read();
    let mut vec:Vec<i64> = read_vec();
    let mut data:Vec<i64> = vec![0; n as usize];
    let mut ma:i64 = 0;
    for i in 0..n {
        data[vec[i as usize] as usize] += 1;
        ma = cmp::max(vec[i as usize],ma);
    }
    let mut flg:bool = false;
    if data[0] != 1 {
        flg = true;
    }
    for i in 0..(ma+1) as usize {
        if data[i] == 0 {
            flg = true;
        }
    }
    if flg == true {
        println!("0");
        return;
    }
    let mut res:u64 = 1;

    // let (f1,f2) = fact_inv_table(101010, MOD as u64);
    
    for i in 0..(ma) {
        let mut x:u64 = 1;
        x =  mod_pow(data[i as usize] as u64, data[(i + 1) as usize] as u64, MOD as u64);
        res *= x;
        res %= MOD as u64;

    }
    println!("{:?}", res);
    



    
}

fn main() {
    solve()
}