#abc179d. [abc179_d]Leaping Tak
[abc179_d]Leaping Tak
Problem Statement
There are cells arranged in a row, numbered from left to right.
Tak lives in these cells and is currently on Cell . He is trying to reach Cell by using the procedure described below.
You are given an integer that is less than or equal to , and non-intersecting segments \[L_1, R_1\], \[L_2, R_2\], \\ldots, \[L_K, R_K\]. Let be the union of these segments. Here, the segment \[l, r\] denotes the set consisting of all integers that satisfy .
- When you are on Cell , pick an integer from and move to Cell . You cannot move out of the cells.
To help Tak, find the number of ways to go to Cell , modulo .
Constraints
- \[L_i, R_i\] and \[L_j, R_j\] do not intersect ()
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of ways for Tak to go from Cell to Cell , modulo .
Sample Input 1
5 2
1 1
3 4
Sample Output 1
4
The set is the union of the segment \[1, 1\] and the segment \[3, 4\], therefore holds.
There are possible ways to get to Cell :
- ,
- ,
- and
- .
Sample Input 2
5 2
3 3
5 5
Sample Output 2
0
Because holds, you cannot reach to Cell . Print .
Sample Input 3
5 1
1 2
Sample Output 3
5
Sample Input 4
60 3
5 8
1 3
10 15
Sample Output 4
221823067
Note that you have to print the answer modulo .