405020: GYM101741 J Subsequence Sum Queries

Memory Limit:0 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

J. Subsequence Sum Queriestime limit per test2 secondsmemory limit per test256 mebibytesinputstandard inputoutputstandard output

You have an array a containing n integers and an integer m. You also have q queries to answer. The i-th query is described as a pair of integers (li, ri). Your task is to calculate the number of such subsequences aj1, aj2, ..., ajk that li ≤ j1 < j2 < ... < jk ≤ ri and . In other words, you need to calculate the number of subsequences of subarray [ali, ali + 1, ..., ari] such that the sum of elements in each subsequence is divisible by m.

Input

The first line contains two integers n and m: the number of elements in a and the modulo (1 ≤ n ≤ 2·105, 1 ≤ m ≤ 20).

The second line contains n integers ai: the elements of array a (0 ≤ ai ≤ 109).

The third line contains one integer q: the number of queries (1 ≤ q ≤ 2·105).

Then q lines follow. The i-th of these lines contains two integers li and ri that describe the i-th query (1 ≤ li ≤ ri ≤ n).

Output

Print q lines. The i-th of them must contain the answer for the i-th query. Queries are indexed in the order they are given in the input. Since the answers can be very large, print them modulo 109 + 7.

ExampleInput
4 3
5 1 3 2
4
1 2
1 3
1 4
2 4
Output
2
4
6
4

加入题单

算法标签: