Description
Chiaki has a sequence A={a1,a2,…,an}. Let RMQ(A,l,r) be the minimum i (l≤i≤r) such that ai is the maximum value in al,al+1,…,ar.
Two sequences A and B are called \textit{RMQ Similar}, if they have the same length n and for every 1≤l≤r≤n, RMQ(A,l,r)=RMQ(B,l,r).
For a given the sequence A={a1,a2,…,an}, define the weight of a sequence B={b1,b2,…,bn} be ∑i=1nbi (i.e. the sum of all elements in B) if sequence B and sequence A are RMQ Similar, or 0 otherwise. If each element of B is a real number chosen independently and uniformly at random between 0 and 1, find the expected weight of B.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106) – the length of the sequence.
The second line contains n integers a1,a2,…,an (1≤ai≤n) denoting the sequence.
It is guaranteed that the sum of all n does not exceed 3×106.
Output
For each test case, output the answer as a value of a rational number modulo 109+7.
Formally, it is guaranteed that under given constraints the probability is always a rational number pq p q (p and q are integer and coprime, q is positive), such that q is not divisible by 1e9+7. Output such integer a between 0 and 1e9+6 that p−aq is divisible by 1e9+7.
Sample Input
3
3
1 2 3
3
1 2 1
5
1 2 3 2 1
Sample Output
250000002
500000004
125000001
Source
2018 Multi-University Training Contest 1
这题现场被卡常了,3e6的nlogn竟然会T,吐槽HDU的评测机
我们发现对于相同的数,RMQ的位置算最左边的那个,相当于对于相同的数,越靠左越大,这样我们可以对A数组进行重新标号,这样可以得到一个和他similar的一个n的排列A
考虑到在0到1之间随机n个数,有数字相等的概率是0,所以可以认为把随机出来的数离散化以后一定是一个n的排列,所以随机到每种排列的概率应该都是