#P7486. [2017年杭电多校]Rikka with Number
[2017年杭电多校]Rikka with Number
Rikka with Number
Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: In radix , a number is good if and only is a permutation of numbers from to . A number is good if and only if there exists at least one and is good under radix . Now, Yuta wants to calculate the number of good numbers in interval It is too difficult for Rikka. Can you help her?
Input
The first line contains a number , the number of the testcases. For each testcase, the first line contains two decimal numbers .
Output
For each testcase, print a single line with a single number -- the answer modulo .
Sample Input
2
5 20
123456 123456789
Sample Output
3
114480
Source
2017 Multi-University Training Contest - Team 5