luogu#P16365. [OOI 2026] Tasks from Sasha

[OOI 2026] Tasks from Sasha

Problem Description

Sasha recently moved into a multi-story building. The building has a total of nn floors, numbered from 11 to nn. There is exactly one resident living on each floor. Between the floors, there are n1n - 1 staircases, and these staircases are not necessarily built between adjacent floors. It is known that for every floor except the first, there is exactly one staircase leading to a lower floor. For the ii-th floor (2in2 \le i \le n), this staircase leads to the floor numbered pip_i.

Sasha plans to solve kk tasks numbered from 11 to kk. For the task with number ii, Sasha has calculated that it is most optimal to solve it on the floor numbered xix_i. Since the tasks are different from each other, all values of xix_i are distinct.

It is boring to solve tasks alone, so for each task, Sasha wants to invite at least one resident to solve it together. However, the residents of the building really dislike climbing stairs and are only willing to descend to the floors where the tasks will be solved. Therefore, Sasha can invite a resident from floor jj to solve task ii only if there is a way to reach floor xix_i from floor jj, using several, possibly zero, staircases that each time lead to a floor with a lower number. Thus, a resident from floor jj can solve task ii only if j=xij = x_i or pj=xip_j = x_i, or ppj=xip_{p_j} = x_i, and so on.

Residents really dislike descending the stairs unnecessarily. Therefore, if Sasha invites a certain set of people to solve a task, they will only be willing to gather to solve the task on the $\textbf{highest floor that they all can descend to}$. For example, if there is a staircase from floor 33 to floor 22, then Sasha cannot invite residents from floors 22 and 33 to solve the task on floor 11, as all residents can gather on a higher floor.

Sasha does not like to appear intrusive, so he will invite a resident from each floor to solve no more than one task. At the same time, he may choose not to invite residents from some floors to solve tasks.

Sasha has one more favorite task that he will not share with anyone except you. But for him to tell you about it, you need to help him count the number of different ways to invite residents to solve tasks with him, ensuring that all restrictions are met. Two ways are considered different if at least one task is solved by a different set of residents.

Input Format

The first line contains two integers nn and kk (3n1063 \le n \le 10^6, 1kmin(n,2000)1 \le k \le \min(n, 2000)) --- the number of floors in the building and the number of tasks.

The second line contains kk integers x1x_1, x2x_2, \ldots, xkx_k (1xin1 \le x_i \le n) --- the floors on which Sasha will solve the tasks. It is guaranteed that all xix_i are distinct.

The third line contains n1n-1 integers p2p_2, p3p_3, \ldots, pnp_n (1pi<i1 \le p_i < i), where pip_i describes the number of the floor to which the staircase leads down from floor ii.

Output Format

Output one number --- the number of different ways to invite residents of the building to solve tasks together with Sasha, modulo 998244353998\,244\,353.

3 1
1
1 1
5
6 2
2 5
1 2 3 4 5
12
7 3
2 7 1
1 1 2 2 3 3
62

Hint

Note

In the first example, Sasha has five ways to invite residents to solve tasks:

  • only with the resident on floor 11;
  • with residents on floors 11 and 22;
  • with residents on floors 11 and 33;
  • with residents on floors 11, 22, and 33;
  • with residents on floors 22 and 33;

Sasha cannot invite the resident from floor 22 alone to solve the task, as then the highest floor where all residents willing to solve the task could gather would be floor 22, while Sasha wants to solve the task on floor 11.

In the second example, two different suitable ways to invite residents to solve tasks could be as follows:

  • Invite residents from floors 22 and 66 to solve the first task, and the resident from floor 55 to solve the second task.
  • Invite the resident from floor 22 to solve the first task, and residents from floors 55 and 66 to solve the second task.

Scoring

The tests for this problem consist of nine groups. Points for each group are awarded only if all tests in the group and all tests in some of the previous groups are passed. Note that passing the tests from the statement is not required for some groups. Offline verification\textbf{Offline verification} means that the results of testing your solution on this group will only be available after the competition ends. The final score for each group is equal to the maximum score obtained for that group of tests across all submitted solutions.

Group Points Additional constraints < Required groups Comment
nn kk
00 -- Tests from the statement.
11 1212 n10n \le 10 k10k \le 10 00
22 1313 n500n \le 500 k500k \le 500 0,10, 1
33 99 -- k=1k = 1 --
44 1010 -- pi=i1p_i = i - 1
55 1313 44 Each floor is connected to no more than two floors with a higher number
66 1414 n200000n \le 200\,000 k500k \le 500 00 -- 22
77 1111 -- 00 -- 3,63, 6
88 1010 k1000k \le 1000 00 -- 3,6,73, 6, 7
99 88 -- 00 -- 88 Offline verification