P12639 [UOI 2020] Topological Sorting of a Tree

Description

You are given a tree with $n$ vertices, numbered from $1$ to $n$. The root of the tree is the vertex with number $1$. For each $v$ from $2$ to $n$, let's define $p_v$ as the number of the vertex adjacent to $v$ that lies on the simple path between vertex $v$ and the root. Each edge $(p_v, v)$ is labeled with the symbol $\tt{}$. Find the number of ways to place the numbers from $1$ to $n$ in the vertices of the tree, such that each number is used exactly once and all the relationships indicated on the edges are satisfied. That is, for all edges with the symbol $\tt{}$, $a[p_v] > a[v]$ should hold. Since the answer can be very large, output it modulo $10^9 + 7$.

Input Format

The first line contains a single integer $n$ ($1 \leq n \leq 3\,000$) - the number of vertices in the tree. Each of the next $n-1$ lines contains a single integer $p_i$ ($1 \leq p_i < i$) and a character $c_i$ ($c_i \in \{\tt{}\}$), indicating that the edge between vertices with indices $p_i$ and $i$ is labeled with the symbol $c_i$. Note that the indexing starts from $2$.

Output Format

Output a single integer - the number of ways to arrange all numbers from $1$ to $n$ in the vertices of the tree such that all the relationships indicated on the edges are satisfied. Note that you should output the remainder of the division by $10^9 + 7$, not the actual answer.

Explanation/Hint

- ($8$ points) $n \leq 10$; - ($6$ points) $n \leq 18$; - ($10$ points) $c_i = \tt{