Under construction

Still being built. Things may change

Subsequence Sum Queries

Time limit2sMemory limit256 MB

Summary
For each query range [l, r] of an array with m at most 20, count the subsequences whose sum is divisible by m, modulo 1e9+7.
Level

Medium6 of 10

Topics
Divide and conquer, Dynamic programming
Solved
No attempts yet

Problem

You are given an array aa of nn integers and an integer mm. You must answer qq queries. The ii-th query is a pair of integers (li,ri)(l_i, r_i). For each query, count the subsequences aj1,aj2,…,ajka_{j_1}, a_{j_2}, \ldots, a_{j_k} with li≤j1<j2<⋯<jk≤ril_i \le j_1 < j_2 < \cdots < j_k \le r_i whose sum aj1+aj2+⋯+ajka_{j_1} + a_{j_2} + \cdots + a_{j_k} is divisible by mm. In other words, count the subsequences of the subarray [ali,ali+1,…,ari][a_{l_i}, a_{l_i+1}, \ldots, a_{r_i}] whose element sum is a multiple of mm.

Input

The first line contains two integers nn and mm (1≤n≤2×1051 \le n \le 2 \times 10^5, 1≤m≤201 \le m \le 20).

The second line contains nn integers aia_i (0≤ai≤1090 \le a_i \le 10^9).

The third line contains one integer qq (1≤q≤2×1051 \le q \le 2 \times 10^5).

Each of the next qq lines contains two integers lil_i and rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n).

Output

Print qq lines. The ii-th line must contain the answer to the ii-th query, in the order the queries are given. Print each answer modulo 109+710^9+7.

Examples1

  1. Example 1

    Input
    4 3
    5 1 3 2
    4
    1 2
    1 3
    1 4
    2 4
    
    Expected output
    2
    4
    6
    4