No Snakes only Ladders

4.5

2 votes
DP
Problem

There are total N+1 squares numbered from 0 to N. You start at square 0. Also there are L ladders, where i-th ladder is denoted by two integers: aibi, it means that the ladder starts at square ai and ends at bi ( ai<bi ). There is no such square where more than one ladder starts, and also, the starting square of any ladder is not the same as the ending square of any other ladder. Now in every move, you can move ahead by  1, 2, 3, 4, 5 or 6 squares. If you land on a square where any ladder starts, then you will climb that ladder and directly get transported to the square where that ladder ends. You cannot move beyond square N, (for e.g., if you are at square N1, then you have only one option - to move to square N).

For each ladder i, you have to find out the number of ways to reach from square 0 to square N, in which you have climbed (used) ladder i. It doesn't matter if you used the other ladders or not. Since the answer can be large, print your answer modulo 109+7.

Input

  • The first line of input contains two space-separated integers: NL, the last square and the number of ladders.
  • Each ith line of next L lines contains two space-separated integers: aibi, the starting and ending squares of ladder i.

Output

Print L lines, the ith of which contains a single integer - the number of ways to reach the square N while having climbed ladder i (modulo 109+7). Also print a new-line at the end.

Constraints

  • 1N105
  • 1L<N
  • For any two ladders i and jaiaj and biaj and bjai
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first ladder, the 4 possible ways are:

013456

01346

01356

0136

for the second ladder, the only possible way is:

0256

Editor Image

?