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 (Clang 3.8.0) (judge verdict: Accepted)

/**
 * Libraries
 */
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <iomanip>
#include <set>
#include <unordered_map>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

/**
 * macros
 */
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define repx(i,s,e,d) for(int i=s,i##_end=(e);i<i##_end;i+=d)
#define repxr(i,s,e,d) for(int i=s,i##_end=(e);i>i##_end;i+=d)
#define rept(n) for(int ___i___=0,i##_len=(n);___i___<i##_len;++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a,0,sizeof(a))
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(),v.end()),v.end());
#define CEIL(x, y) (((x) + (y) - 1) / (y))

/**
 * standard input helpers
 */
// vector
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// vector
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
    os << "{";
    repi(itr, map_var) {
        os << *itr;
        itr++;
        if (itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
    os << "{";
    repi(itr, set_var) {
        os << *itr;
        itr++;
        if (itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
/**
 * debug tools
 */
#define DUMPOUT cout
void dump_func() {
    DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) {
        DUMPOUT << ", ";
    }
    dump_func(std::move(tail)...);
}
#ifdef LOCAL_
#define dump(...)                                                              \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
            << endl                                                            \
            << "    ",                                                         \
        dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

/**
 * helpers
 */
typedef long long int lli;
typedef pair<int, int> ii;
typedef priority_queue<int, vector<int>, greater<int> > heapq;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct aaa{
    aaa(){
        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    };
}aaaaaaa;
template<int MOD> struct Modnum {
    lli val;
    constexpr Modnum(lli v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }

    constexpr Modnum operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Modnum operator + (const Modnum& x) const noexcept {
        return Modnum(*this) += x;
    }
    constexpr Modnum operator - (const Modnum& x) const noexcept {
        return Modnum(*this) -= x;
    }
    constexpr Modnum operator * (const Modnum& x) const noexcept {
        return Modnum(*this) *= x;
    }
    constexpr Modnum operator / (const Modnum& x) const noexcept {
        return Modnum(*this) /= x;
    }

    constexpr Modnum& operator += (const Modnum& x) noexcept {
        val += x.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Modnum& operator -= (const Modnum& x) noexcept {
        val -= x.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Modnum& operator *= (const Modnum& x) noexcept {
        val = val * x.val % MOD;
        return *this;
    }
    constexpr Modnum& operator /= (const Modnum& x) noexcept {
        val = val * modinv(x.val).val % MOD;
        return *this;
    }

    constexpr bool operator == (const Modnum& x) const noexcept {
        return val == x.val;
    }
    constexpr bool operator != (const Modnum& x) const noexcept {
        return val == x.val;
    }

    friend constexpr ostream& operator << (ostream &os, const Modnum<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, const Modnum<MOD>& x) noexcept {
        return is >> x.val;
    }
    static constexpr Modnum<MOD> modpow(const Modnum &x, lli n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(x, n / 2);
        t *= t;
        if (n & 1) t *= x;
        return t;
    }
    static constexpr Modnum<MOD> modinv(const Modnum &x) noexcept {
        return modpow(x, MOD - 2);
    }
};

using mi = Modnum<998244353>;
#define int long long int
signed main() {
    int N; cin >> N;
    vector<int> D(N); cin >> D;

    if (D[0] != 0) {
        cout << 0 << endl;
        return 0;
    }

    vector<int> Cnt(N, 0);
    rep(i, N) {
        if (i > 0 && D[i] == 0) {
            cout << 0 << endl;
            return 0;
        }
        Cnt[D[i]]++;
    }
    repx(i, 2, N, 1) {
        if (Cnt[i] != 0 && Cnt[i - 1] == 0) {
            cout << 0 << endl;
            return 0;
        }
    }
    dump(Cnt);

    mi ans(1);
    repx(i, 2, N, 1) {
        if (Cnt[i] == 0) break;
        ans *= mi::modpow(mi(Cnt[i - 1]), Cnt[i]);
    }
    cout << ans << endl;
    return 0;
}