#P11159. 【MX-X6-T5】 再生

【MX-X6-T5】 再生

Background

Original problem link: https://oier.team/problems/X6F


このまま\\ らったった\\ 音に乗って\\ 今きっと世界で僕だけだ\\ 後ろ向きな歌を聴いて\\ 少しだけ\\ 前向きに生きていく

—— 再生 - Nanatsukaze

Broken points are recombined according to broken rules. What kind of structure will be “regenerated” in this way?

Problem Description

You are given a labeled rooted tree with nn nodes, and the array top obtained from its long-chain decomposition. Please output how many different trees can produce this top array after long-chain decomposition. The answer should be taken modulo 2005113120051131 (a prime).

More precisely, for a tree TT, define the height huh_u of every node uu:

  • If uu is a leaf, then hu=1h_u = 1
  • Otherwise, let the set of children of uu be SuS_u, then hu=maxvSuhv+1h_u = \max\limits_{v \in S_u} h_v + 1

Given an array t1nt_{1\cdots n}, you need to count how many trees satisfy:

  • For the root node rr, tr=rt_r = r
  • For every non-leaf node uu, there exists exactly one child vv such that hv+1=huh_v + 1 = h_u and tv=tut_v = t_u, and every other child satisfies tv=vt_v = v

Take the result modulo 2005113120051131 (a prime).

Two trees are different if and only if their roots are different or their edge sets are different.

It is guaranteed that the answer is not 0\bf 0 before taking modulo, but it is not guaranteed to be non-zero modulo 2005113120051131.

Input Format

The first line contains a positive integer nn

The next line contains nn positive integers t1nt_{1\cdots n} separated by spaces, representing the top array.

Output Format

Output one integer: the answer modulo 2005113120051131

5
1 1 1 4 4
2
16
1 2 1 4 1 4 1 4 9 1 1 12 1 1 12 1
7181107

Hint

Sample Explanation #1

Only the two trees in the figure satisfy the conditions.

Constraints

For all testdata, it is guaranteed that 1n5×1051 \leq n \leq 5 \times 10^51tii1 \leq t_i \leq i,and the answer before taking modulo is not 00

Bundled test: 5 subtasks, with the following limits:

  • Subtask 1 (11 pts): ti=1t_i = 1
  • Subtask 2 (24 pts): n5n \leq 5
  • Subtask 3 (17 pts): n16n \leq 16
  • Subtask 4 (22 pts): n2×103n \leq 2 \times 10^3
  • Subtask 5 (26 pts): no special restrictions.

Translated by ChatGPT 5