atcoder#NIKKEI20192QUALB. Counting of Trees

Counting of Trees

Score : 300300 points

Problem Statement

Given is an integer sequence D1,...,DND_1,...,D_N of NN elements. Find the number, modulo 998244353998244353, of trees with NN vertices numbered 11 to NN that satisfy the following condition:

  • For every integer ii from 11 to NN, the distance between Vertex 11 and Vertex ii is DiD_i.

Notes

  • A tree of NN vertices is a connected undirected graph with NN vertices and N1N-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 xx and yy such that there is an edge between xx and yy in one of those trees and not in the other.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 0DiN10 \leq D_i \leq N-1

Input

Input is given from Standard Input in the following format:

NN

D1D_1 D2D_2 ...... DND_N

Output

Print the answer.

4
0 1 1 2
2

For example, a tree with edges (1,2)(1,2), (1,3)(1,3), and (2,4)(2,4) satisfies the condition.

4
1 1 1 1
0
7
0 3 2 1 2 2 1
24