#P5860. 「SWTR-3」Counting Trees

「SWTR-3」Counting Trees

Description

There are nn vertices, and each vertex has a weight viv_i.

Little S\mathrm{S} wants Little A\mathrm{A} to choose some vertices to form a set. Let the set be S={d1,d2,,dm}(1mn)S=\{d_1,d_2,\dots,d_m\}(1\leq m\leq n).

Of course, Little A\mathrm{A} also needs to ensure that these vertices can form a tree, and the degree of did_i is vdi(i[1,m])v_{d_i}(i\in[1,m]).

  • Degree of a vertex: the number of vertices adjacent to it.

Little S\mathrm{S} asks Little A\mathrm{A} how many ways there are that satisfy the conditions.

Little A\mathrm{A} knows he definitely cannot solve this problem, so he asks you for help.

Since the number of ways may be very large, output it modulo 998244353998244353.

Input Format

The first line contains an integer nn.

The second line contains nn integers v1,v2,,vnv_1,v_2,\dots,v_n.

Output Format

Output one integer in one line, representing the number of valid ways.

3
1 1 1

3
5
1 2 1 3 1

8
8
1 2 1 2 4 1 3 1

44
50
8 1 10 2 2 1 2 1 1 2 5 1 11 6 13 13 10 4 1 13 11 2 2 11 13 10 1 1 4 3 4 2 15 2 2 1 1 2 1 7 14 2 2 4 13 2 7 5 6 10 
176873472

Hint


Sample Explanation

  • For sample 11, it is enough to choose any two vertices among the three vertices. The answer is C32=3C^{2}_{3}=3.

  • For sample 22, as shown in the figure, there are 88 ways to choose vertices.


Constraints and Notes

This problem uses bundled testdata.

Subtask ID nn\leq Special Property Score
11 2020 None 1111
22 5050 1212
33 300300 1010
44 25002500 1717
55 4×1044\times 10^4 66
66 3×1053\times 10^5 vi3v_i\leq 3 88
77 Random testdata 77
88 5×1055\times 10^5 None 2929

For 100%100\% of the testdata, 2n5×1052 \leq n \leq 5 \times 10^5 and  1vin\ 1 \leq v_i \leq n.


In Subtask 7\mathrm{Subtask\ 7}, “random testdata” means: for every viv_i, it equals 11 with probability 13\frac{1}{3}, and with probability 23\frac{2}{3} it is chosen uniformly at random from [2,n][2,n].


For the first 44 Subtask\mathrm{Subtask}s, the time limit is 1s1\mathrm{s}.

For the 55th Subtask\mathrm{Subtask}, the time limit is 3s3\mathrm{s}.

For the last 33 Subtask\mathrm{Subtask}s, the time limit is 6s6\mathrm{s}.

For all test points, the memory limit is 256MB256\mathrm{MB}.

Translated by ChatGPT 5