Non-Decreasing Subsequences

여러 구간 질의마다 그 구간에서 감소하지 않는 부분수열의 개수(빈 부분수열 포함)를 10^9+7로 나눈 나머지를 구한다.

어려움8동적 계획법분할 정복세그먼트 트리행렬아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

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

Consider a sequence A_1,A_2,,A_NA\_1,A\_2,\ldots,A\_N of length NN (1N5104)(1\le N\le 5\cdot 10^4) consisting solely of integers in the range 1K1\ldots K (1K20).(1\le K\le 20). You are given QQ (1Q21051\le Q\le 2\cdot 10^5) queries of the form \[L_i,R_i]\[L\_i,R\_i] (1L_iR_iN).(1\le L\_i\le R\_i\le N). For each query, compute the number of non-decreasing subsequences of A_L_i,A_L_i+1,A_R_iA\_{L\_i},A\_{L\_i+1}\ldots, A\_{R\_i} mod 109+710^9+7.

A non-decreasing subsequence of A_L,,A_RA\_L,\ldots,A\_R is a collection of indices (j_1,j_2,,j_x)(j\_1,j\_2,\ldots, j\_x) such that Lj_1\<j_2<\<j_xRL\le j\_1\<j\_2<\cdots\<j\_x\le R and A_j_1A_j_2A_j_x.A\_{j\_1}\le A\_{j\_2}\le \cdots \le A\_{j\_x}. Make sure to consider the empty subsequence!

입력

The first line contains two space-separated integers NN and KK.

The second line contains NN space-separated integers A_1,A_2,,A_NA\_1,A\_2,\ldots, A\_N.

The third line contains a single integer Q.Q.

The next QQ lines each contain two space-separated integers L_iL\_i and R_i.R\_i.

출력

For each query \[L_i,R_i],\[L\_i,R\_i], you should print the number of non-decreasing subsequences of A_L_i,A_L_i+1,A_R_iA\_{L\_i},A\_{L\_i+1}\ldots, A\_{R\_i} mod 109+710^9+7 on a new line.

힌트

For the first query, the non-decreasing subsequences are (),(2),(), (2), and (3).(3). (2,3)(2,3) is not a non-decreasing subsequence because A_2≰A_3.A\_2\not \le A\_3.

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