Gats Between the Lookouts
Gats has found platforms arranged in a line and numbered from left to right. Platform has height , and one fish is waiting there.
Gats visits every platform exactly once. His visiting order is chosen uniformly at random from all possible orders.
When Gats visits platform , consider only platforms visited earlier:
- If there is a previously visited platform to the left, let be the greatest previously visited index satisfying .
- If there is a previously visited platform to the right, let be the least previously visited index satisfying .
Gats catches the fish at platform if both and exist and
Otherwise, that landing catches no fish. Each platform is visited only once, so each fish can be caught at most once.
Find the expected total number of fish Gats catches.
Input
The first line contains an integer (), the number of platforms.
The second line contains integers (), where is the height of platform . Heights are not necessarily distinct.
Output
Print one integer: the expected number of fish caught, modulo .
Formally, if the expectation is represented as , where is not divisible by , print
Samples
Sample 1
Input
3 1 2 3
Output
332748118
The expected number of fish caught is , whose required residue is .
Sample 2
Input
5 2 5 3 1 4
Output
765320671
For these platform heights, the expected number of fish caught is .