RMQ Similar Sequence

수열 A가 주어질 때, [0,1] 위의 균등분포에서 독립적으로 뽑은 수열 B가 A와 모든 구간에서 최댓값 위치가 같을 조건 아래 B 원소 합의 기댓값을 구한다.

어려움9트리확률조합론DFS아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

Chiaki has a sequence A=a_1,a_2,,a_nA=\\{a\_1,a\_2,\dots,a\_n\\}. Let RMQ(A,l,r)\mathbf{RMQ}(A, l, r) be the minimum ii (lirl \le i \le r) such that a_ia\_i is the maximum value in a_l,a_l+1,,a_ra\_l, a\_{l+1}, \dots, a\_{r}.

Two sequences AA and BB are called RMQ Similar, if they have the same length nn and for every 1lrn1 \le l \le r \le n, RMQ(A,l,r)=RMQ(B,l,r)\mathbf{RMQ}(A, l, r) = \mathbf{RMQ}(B, l, r).

For a given the sequence A=a_1,a_2,,a_nA=\\{a\_1,a\_2,\dots,a\_n\\}, define the weight of a sequence B=b_1,b_2,,b_nB=\\{b\_1,b\_2,\dots,b\_n\\} be _i=1nb_i\sum\limits\_{i=1}^{n} b\_i (i.e. the sum of all elements in BB) if sequence BB and sequence AA are RMQ Similar, or 00 otherwise. If each element of BB is a real number chosen independently and uniformly at random between 00 and 11, find the expected weight of BB.

입력

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1n1061 \le n \le 10^6) -- the length of the sequence.

The second line contains nn integers a_1,a_2,,a_na\_1, a\_2, \dots, a\_n (1a_in1 \le a\_i \le n) denoting the sequence.

It is guaranteed that the sum of all n does not exceed 3×1063 \times 10^6.

출력

For each test case, output the answer as a value of a rational number modulo 109+710^9 + 7.

Formally, it is guaranteed that under given constraints the probability is always a rational number pq\frac{p}{q} (pp and qq are integer and coprime, qq is positive), such that qq is not divisible by 109+710^9 + 7. Output such integer a between 00 and 109+610^9 + 6 that paqp - aq is divisible by 109+710^9 + 7.