#abc239h. [abc239_h]Dice Product 2
[abc239_h]Dice Product 2
Problem Statement
Snuke has a die (singular of dice) that shows integers from through with equal probability, and an integer .
He repeats the following operation while his integer is less than or equal to .
- He rolls the die. If the die shows an integer , he multiplies his integer by .
Find the expected value of the number of times he rolls the die until he stops, modulo .
Definition of the expected value modulo We can prove that the desired expected value is always a rational number. Moreover, under the constraints of the problem, when the value is represented as an irreducible fraction , we can also prove that . Thus, an integer such that and is uniquely determined. Answer such .
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
2 1
Sample Output 1
2
The answer is the expected value of the number of rolls until it shows for the first time. Thus, should be printed.
Sample Input 2
2 39
Sample Output 2
12
The answer is the expected value of the number of rolls until it shows six times. Thus, should be printed.
Sample Input 3
3 2
Sample Output 3
250000004
The answer is . We have , so should be printed.
Note that the answer should be printed modulo .
Sample Input 4
2392 39239
Sample Output 4
984914531
Sample Input 5
1000000000 1000000000
Sample Output 5
776759630