
ZOJ Problem Set  3929
There are n balls, where the ith ball is labeled as p_{i}. You are going to put n balls into a deque. In the ith turn, you need to put the ith ball to the deque. Each ball will be put to both ends of the deque with equal probability. Let the sequence (x_{1}, x_{2}, ..., x_{n}) be the labels of the balls in the deque from left to right. The beauty of the deque B(x_{1}, x_{2}, ..., x_{n}) is defined as the number of descents in the sequence. For the sequence (x_{1}, x_{2}, ..., x_{n}), a descent is a position i (1 ≤ i < n) with x_{i} > x_{i+1}. You need to find the expected value of B(x_{1}, x_{2}, ..., x_{n}). Deque is a doubleended queue for which elements can be added to or removed from either the front (head) or the back (tail). InputThere 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 (2 ≤ n ≤ 100000)  the number of balls. The second line contains n integers: p_{1}, p_{2}, ..., p_{n} (1 ≤ p_{i} ≤ n). OutputFor each test case, if the expected value is E, you should output E⋅2^{n} mod (10^{9} + 7). Sample Input2 2 1 2 3 2 2 2 Sample Output2 0 Author: LIN, Xi Source: The 16th Zhejiang University Programming Contest 