atcoder#ABC276F. [ABC276F] Double Chance
[ABC276F] Double Chance
Score : points
Problem Statement
There are cards called card , card , , card . On card , an integer is written.
For , solve the following problem.
We have a bag that contains cards: card , card , , card . Let us perform the following operation twice, and let and be the numbers recorded, in the recorded order.
Draw a card from the bag uniformly at random, and record the number written on that card. Then, return the card to the bag.
Print the expected value of , modulo (see Notes). Here, denotes the value of the greater of and (or if they are equal).
Notes
It can be proved that the sought expected value is always finite and rational. Additionally, under the Constraints of this problem, when that value is represented as with two coprime integers and , it can be proved that there is a unique integer such that and . Print this .
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer to the problem for .
3
5 7 5
5
499122183
443664163
For instance, the answer for is found as follows.
The bag contains card and card , with and written on them, respectively.
- If you draw card in the first draw and card again in the second draw, we have , so .
- If you draw card in the first draw and card in the second draw, we have and , so .
- If you draw card in the first draw and card in the second draw, we have and , so .
- If you draw card in the first draw and card again in the second draw, we have , so .
These events happen with the same probability, so the sought expected value is . We have , so should be printed.
7
22 75 26 45 72 81 47
22
249561150
110916092
873463862
279508479
360477194
529680742