Consecutive Neighbours
A uniformly random permutation π of 1,2,…,n is generated. For each i=1,2,…,n−1, if the values i and i+1 appear in adjacent positions of π, you score wi.
Let X be the total score. Output the expected value of X.
It can be shown that the answer can be expressed as a rational number P/Q in lowest terms with Q coprime to 109+7. Output P⋅Q−1mod(109+7).
Input
The first line contains an integer n.
The second line contains n−1 integers w1,w2,…,wn−1. If n=1, this line is empty.
Output
Print a single integer: the expected total score modulo 109+7.
Constraints
- 1≤n≤2⋅105
- 0≤wi≤109
Example 1
3
1 1
333333337
Explanation
There are two pairs, (1,2) and (2,3). Each pair of consecutive values occupies adjacent positions with probability 2/3, so the expected score is 4/3. 4⋅3−1≡333333337(mod109+7).
Example 2
2
10
10
Explanation
The two values are always adjacent, so the score is always 10.
Example 3
1
0
Explanation
There are no consecutive pairs when n=1.
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.