Expected Value

Time limit3sMemory limit16 MB

Summary
Repeatedly pick a random adjacent pair, replace the left value by its difference with the right, drop the right. Find the expected final value modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Probability, Combinatorics, Math
Solved
No attempts yet

Problem

Here is a game played with sequence a1,…,ana_1, \dots, a_n. On each turn, the player chooses some position i<ni < n uniformly at random, replaces the element aia_i with ai−ai+1a_i - a_{i+1}, and then removes the element ai+1a_{i+1} from the sequence. This continues until there is only one element left. What is the expected value of the last element?

Input

The first line of input contains a single integer nn (2≤n≤40002 \le n \le 4000).

The second line of input contains nn integers a1,…,ana_1, \dots, a_n (1≤ai≤40001 \le a_i \le 4000).

Output

If the answer is P/QP/Q such that PP and QQ are coprime, output a single integer which is (P⋅Q−1) mod (109+7)(P \cdot Q^{-1}) \bmod (10^9 + 7). It is guaranteed that Q≢0(mod109+7)Q \not\equiv 0 \pmod{10^9 + 7}.

Hint

Pay attention to the non-standard memory limit.

Examples1

  1. Example 1

    Input
    2
    2 1
    
    Expected output
    1