#P10789. Climb Because The Mountain Is There
Climb Because The Mountain Is There
Climb Because The Mountain Is There
"Why climb? Because the mountain is there."
You are given a rooted tree with n nodes representing various points on Mushtagh Mountain. The nodes are numbered from 1 to n with node 1 being the mountain top. For each node i (2 ≤ i ≤ n), its parent is given as pi. Define di as the number of edges from node i to the top (formally, d1 = 0 and for 2 ≤ i ≤ n, di = dpi + 1).
A climbing path is a procedure that starts from any node in {2, 3, …, n} and ends at the mountain top (node 1) through a sequence of moves. The corresponding climbing sequence is a sequence of nodes a1, a2, …, am with a1 being the starting node and am = 1.
There are two types of moves allowed from any node i (2 ≤ i ≤ n):
- Sprint: You are given a sprint range [li, ri] for node i. In a sprint move, you choose an integer k with li ≤ k ≤ ri and move upward exactly k steps, i.e. to the k-th ancestor of node i. It is guaranteed that 1 ≤ li ≤ ri ≤ di.
- Rest: Due to the treacherous terrain, when you rest you slide down to one of the children. Formally, you choose a node j satisfying pj = i and move to node j. (If node i is a leaf, you cannot choose this move.)
In addition, for each node (either the starting node or one reached after a move) a parameter hi is given with 0 ≤ hi < di. For a climbing sequence a1, a2, …, am to represent a legal climbing path, the following must hold: For all indices 1 ≤ i < j ≤ m, if the move from ai to aj is a sprint move (i.e. it is not a rest move), then
Your task is to compute the total number of legal climbing paths starting from every node numbered from 2 through n. Two climbing paths are considered distinct if their climbing sequences differ. Since the answer can be very large, please output it modulo $$998\,244\,353$$.
inputFormat
The input begins with a single integer n denoting the number of nodes in the tree.
The second line contains n - 1 space-separated integers: p2, p3, …, pn, where pi is the parent of node i.
Then, for each node i from 2 to n, there is a line containing three integers: li, ri, hi.
outputFormat
Output a single integer: the total number of legal climbing paths starting from nodes 2 to n, taken modulo $$998\,244\,353$$.
sample
3
1 1
1 1 0
1 1 0
2