#P1810G. The Maximum Prefix
The Maximum Prefix
No submission language available for this problem.
Description
You're going to generate an array with a length of at most , where each equals either or .
You generate this array in the following way.
- First, you choose some integer (), which decides the length of .
- Then, for each (), you set with probability , otherwise set (with probability ).
After the array is generated, you calculate . Specially, . Then you let equal to . That is, is the maximum prefix sum of the array .
You are given integers . The score of an array with maximum prefix sum is . Now, for each , you want to know the expected score for an array of length modulo .
Each test contains multiple test cases. The first line contains a single integer () — the number of test cases. Their description follows.
The first line contains an integer ().
Then for the following lines, each line contains two integers and (, , ), indicating .
The next line contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output integers in one single line, the -th of which denotes the expected score for an array of length , modulo .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output such an integer that and .
Input
Each test contains multiple test cases. The first line contains a single integer () — the number of test cases. Their description follows.
The first line contains an integer ().
Then for the following lines, each line contains two integers and (, , ), indicating .
The next line contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output integers in one single line, the -th of which denotes the expected score for an array of length , modulo .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output such an integer that and .
Note
In the first test case, if we choose , there are possible arrays with equal probabilities: and . The values for them are and . So the expected score is . If we choose , there are possible arrays with equal probabilities: , , , , and the values for them are . So the expected score is .
In the second test case, no matter what the value is, the score is always , so the expected score is always .