#P15862. [LBA-OI R3 B] 回去!
[LBA-OI R3 B] 回去!
Description
We have a directed graph with nodes and edges, where each node has out-degree exactly . Each node is assigned a lowercase letter as its weight.
DZ1000 is about to walk on . He uses a string and a stack to record his journey. Initially, the stack is empty.
In each step, he has two choices of action:
- Move forward: With probability , he moves along the outgoing edge of the current node and pushes the reached node into stack .
- Go back: With probability , he pops the top node from stack and returns to the node that is now the top of stack . (If stack is empty before this action, he cannot do this and must move forward; if the size of stack is before this action, he returns to the starting node.)
Immediately after each step, DZ1000 appends the weight of the current node to the string .
DZ1000 randomly selects a starting node on with equal probability, then takes exactly steps. You need to find the probability that the first characters of are equal to the last characters after steps. Multiply the answer by and take it modulo .
Note: The weight of the starting node is NOT the first character of string .
Input Format
The input contains exactly lines.
The first line has three positive integers , representing the number of nodes in , the number of steps taken, and the probability of moving forward (originally a rational number between and , given modulo ).
The second line is a string of lowercase letters with length . The -th letter is the weight of node .
The third line has positive integers . The -th integer is the node that the outgoing edge of node points to.
Output Format
Output a single integer: the result of multiplying the required probability by , modulo .
3 1 499122177
aba
2 3 1
1
10 10 882315098
nqqmlmnnon
6 3 4 1 4 4 9 4 10 7
106458929
Hint
Explanation of Sample 1
modulo is , so the original .
The paths are as follows:
- , probability , string
ba— invalid - , probability , string
ba— invalid - , probability , string
aa— valid - , probability , string
ab— invalid - , probability , string
ab— invalid - , probability , string
aa— valid
Total valid probability: . Multiply by to get , so output .
Data Constraints
For data: .
| Subtask ID | Special Property | Score | ||
|---|---|---|---|---|
| None | ||||
| Unrestricted | ^ | ^ | ||
| ^ | ||||
| Unrestricted | A | |||
| ^ | B | |||
| C | ||||
| None | ||||
Special Property A: All are identical.
Special Property B: For all , .
Special Property C: For all , , and .
京公网安备 11011102002149号