Consider a graph organized into a grid of nodes and edges, where each row has nodes and there is an infinite number of rows. In this graph, node
refers to the
-th node in the
-th row. A given bipartition function will add
directed edges per row, each of which joins the
-th node in that row (row
) to the
-th node in the row immediately after (row
).
Someone who lives on this graph is interested in travelling to various rows. He can only start his trip from some node in row , and sets the condition that, in order to reach any destination node, he must start his travels from node
such that
is minimized. Formally, he will not travel from node
to any destination
if there exists at least one path from node
to
, and
. To plan his trip to a desired row, he would like to reflect on how many distinct sequences of nodes he could travel through in order to reach his destination.
Help him by answering queries:
For every node in row
, how many distinct ways modulo
![]()
are there to travel from the desired node in row
to this node?
Two ways are considered distinct if they pass through different sequences of nodes. Note that in some rows, the desired starting point (in row ) may be different for different nodes.
Input Specification
On the first line, read the three integers ,
, and
.
On each of the next lines, read the two integers
and
.
On each of the next lines, read one integer
.
Output Specification
For each query, print one line containing the answers for nodes to
in row
, with each answer separated by a single space.
Subtasks and Constraints
For all subtasks:
Subtask 1 [5%]
Subtask 2 [10%]
Subtask 3 [35%]
Subtask 4 [50%]
No additional constraints.
Sample Input 1
4 5 3
1 2
2 1
3 3
3 4
4 4
2
3
4
Sample Output 1
1 1 1 1
1 1 1 2
1 1 1 3
Explanation for Sample 1
The graph is as illustrated below.
For node , the two distinct ways are to travel from
to
and from
to
before reaching it. Note that it is also reachable from
, but that is irrelevant, as
is a lower numbered node in the first row.
For node , the three ways are (
), (
), and (
).
Sample Input 2
3 5 2
1 1
1 2
2 2
2 3
3 1
3
4
Sample Output 2
1 2 1
2 3 2
Comments