#P7729. [2019年杭电多校]Kejin Player
[2019年杭电多校]Kejin Player
Kejin Player
Problem Description
Cuber QQ always envies those Kejin players, who pay a lot of RMB to get a higher level in the game. So he worked so hard that you are now the game designer of this game. He decided to annoy these Kejin players a little bit, and give them the lesson that RMB does not always work. This game follows a traditional Kejin rule of "when you are level , you have to pay RMB to get to level ". Cuber QQ now changed it a little bit: "when you are level , you pay RMB, are you get to level with probability ; otherwise you will turn into level ()". Cuber QQ still needs to know how much money expected the Kejin players needs to ``ke'' so that they can upgrade from level to level , because you worry if this is too high, these players might just quit and never return again.
Input
The first line of the input is an integer , denoting the number of test cases. For each test case, there is two space-separated integers () and () in the first line, meaning the total number of levels and the number of queries. Then follows lines, each containing integers , , , (, , ), space separated. Note that is given in the form of a fraction . The next lines are queries. Each of these queries are two space-separated integers and (). The sum of and sum of from all test cases both does not exceed .
Output
For each query, output answer in the fraction form modulo , that is, if the answer is , you should output modulo , where denotes the multiplicative inverse of modulo .
Sample Input
1
3 2
1 1 1 2
1 2 1 3
1 3 3 4
1 4
3 4
Sample Output
22
12
Hint
Huge IO (Over 40MB)! IO optimization is preferred.
Source
2019 Multi-University Training Contest 7