The king wants to eat a feast.
The food items will be placed in front of the king in a row, and the king eats the food items in the sequence (left to right) one by one.
He wants to eat different food items in groups of different sizes.
There are N food items and an array A consisting of N elements, where Ai denotes the size of the group in which the king wants to eat the ith food item.
Note:-
Also, the king wants to eat food items in a particular range only.
There would be Q queries, each containing two integers Xi and Yi, denoting the minimum and maximum number of food items that the king wants to eat.
For each query, you need to calculate the total number of ways in which you can create a feast by arranging the food items in such a way that the king gets to eat the food items in groups of sizes desired by him, and the total food items eaten are between Xi and Yi (both inclusive).
The answer should be reported as modulo 109+7 as the number of feasts can be large.
Input Format:
Output Format:
Constraints:
1≤N≤1001≤Ai≤1051≤Q≤1051≤Xi,Yi≤105Xi≤Yi
The different feasts possible are:-
(Here i denotes food item i)
size 1:- no possible feast
size 2:- {1,1}
size 3:- {2,2,2}
size 4:- {1,1,1,1}
size 5:- {3,3,3,3,3},{1,1,2,2,2},{2,2,2,1,1}
size 6:- {1,1,1,1,1,1},{2,2,2,2,2,2}
so, for the first query [1,4] :-
the total no of feasts that the king can eat if he wants to eat food items between 1 and 4 is equal to:-
feasts of size 1 +feasts of size 2 + feasts of size 3 + feasts of size 4 = 0+1+1+1 = 3;
so, for the second query [2,5] :-
the total no of feasts that the king can eat if he wants to eat food items between 2 and 5 is equal to:-
feasts of size 2 +feasts of size 3 + feasts of size 4 + feasts of size 5 = 1+1+1+3 = 6;