Non-Decreasing Subsequences#10440

Non-Decreasing Subsequences

1 second
128 megabytes
Very hard

Bessie recently participated in a USACO contest and encountered the following problem. Of course, she knows how to solve it. But do you?

Consider a sequence A_1, A_2, \ldots, A_n of length , consisting entirely of integers in the range to . You are given queries of the form [l_i, r_i]. For each query, compute the number of non-decreasing subsequences of A_{l_i}, A_{l_i+1}, \ldots, A_{r_i}, modulo 10^9 + 7.

A non-decreasing subsequence of A_l, \ldots, A_r is a sequence of indices (j_1, j_2, \ldots, j_x) such that l \leq j_1 < j_2 < \ldots < j_x \leq r and A_{j_1} \leq A_{j_2} \leq \ldots \leq A_{j_x}. Make sure to include the empty subsequence!

Input

The first line contains two integers n~(1 \le n \le 5 \cdot 10^4) and k~(1 \le k \le 20).

The second line contains integers A_1, A_2, \ldots, A_n.

The third line contains a single integer q~(1 \le q \le 2 \cdot 10^5). Each of the next lines contains two integers l_i and r_i~(1 \le l_i \le r_i \le n).

Output

For each query [l_i, r_i], print the number of non-decreasing subsequences of A_{l_i}, A_{l_i+1}, \ldots, A_{r_i}, modulo 10^9 + 7, on a new line.

Examples

For the first query, the non-decreasing subsequences are , and . The subsequence is not non-decreasing because A_2 > A_3.

For the second query, the non-decreasing subsequences are (), (4), (5), and .

Example 1

Loading
Loading