D - Smallest Vertices Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700700

問題文

この問題では、根付き有向木と言った際には全ての辺が根から葉の方向に向き付けられた根付き木を指すものとします。

総和が N1N-1 であるような非負整数列 d=(d1,d2,,dN)d=(d_1,d_2,\ldots,d_N) が与えられます。

頂点に 11 から NN の番号がついた、頂点 11 を根とする NN 頂点の根付き有向木のうち、以下の条件を満たすものを良い木と呼びます。

  • 頂点 i (1iN)i\ (1\leq i \leq N) の出次数は did_i

さらに、良い木の頂点 vv に対して、 f(v)f(v) を「頂点 vv の部分木に含まれる頂点(vv 含む)の頂点番号の最小値」と定め、f(v)=vf(v)=v を満たす頂点を良い頂点と呼びます。

良い木全てに対する良い頂点の個数の総和を 998244353998244353 で割ったあまりを求めてください。

制約

  • 2N5002 \leq N \leq 500
  • 0diN10 \leq d_i \leq N-1
  • d11d_1 \geq 1
  • i=1Ndi=N1\sum_{i=1}^N d_i = N-1
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN
d1d_1 d2d_2 \ldots dNd_N

出力

答えを出力せよ。


入力例 1Copy

Copy
4
2 0 1 0

出力例 1Copy

Copy
7

良い木は以下の 22 通りあります。青く塗られた頂点は良い頂点です。

それぞれについて良い頂点は 44 個、 33 個なので答えは 77 です。


入力例 2Copy

Copy
10
3 1 0 0 2 0 1 2 0 0

出力例 2Copy

Copy
37542

Score : 700700 points

Problem Statement

In this problem, a rooted directed tree is a rooted tree where all edges are directed from the root to the leaves.

You are given a sequence of non-negative integers d=(d1,d2,,dN)d=(d_1,d_2,\ldots,d_N) with a sum of N1N-1.

Among the NN-vertex rooted directed trees with vertex numbered 11 to NN and vertex 11 as the root, a good tree is one that satisfies the following condition:

  • the out-degree of vertex i (1iN)i\ (1\leq i \leq N) is did_i.

Furthermore, for a vertex vv of a good tree, let f(v)f(v) be the minimum vertex number of the vertices (including vv) in the subtree rooted at vertex vv, and vv is called a good vertex if it satisfies f(v)=vf(v)=v.

Find the sum of the numbers of good vertices for all good trees, modulo 998244353998244353.

Constraints

  • 2N5002 \leq N \leq 500
  • 0diN10 \leq d_i \leq N-1
  • d11d_1 \geq 1
  • i=1Ndi=N1\sum_{i=1}^N d_i = N-1
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN
d1d_1 d2d_2 \ldots dNd_N

Output

Print the answer.


Sample Input 1Copy

Copy
4
2 0 1 0

Sample Output 1Copy

Copy
7

There are two good trees, as shown below. The blue vertices are good vertices.

For these trees, there are 44 and 33 good vertices, respectively, so the answer is 77.


Sample Input 2Copy

Copy
10
3 1 0 0 2 0 1 2 0 0

Sample Output 2Copy

Copy
37542


2025-03-29 (Sat)
14:48:27 +00:00