Collars on the Waking String
There are cats sitting at fixed positions along a taut string. The cat at position wears a collar of color .
The cats wake up one at a time. Their wake order is chosen uniformly at random from all permutations. Once a cat wakes, it remains awake.
When the cat at position wakes, it looks for cats that woke earlier:
- If there is no previously awakened cat to its left or no previously awakened cat to its right, it earns no purr.
- Otherwise, let be the largest previously awakened position smaller than , and let be the smallest previously awakened position larger than . The cat earns one purr if both and hold.
Let be the total number of purrs earned after every cat has awakened. Compute modulo .
Input
The first line contains an integer (), the number of cats.
The second line contains integers (), where is the collar color of the cat at position .
Output
Print the expected value of modulo .
More precisely, if for integers and with , print
Samples
Sample 1
Input
1 7
Output
0
With only one cat, no purr can be earned, so the expected total is .
Sample 2
Input
3 1 2 1
Output
332748118
The expected number of purrs is , represented modulo as .
Sample 3
Input
4 1 2 1 1
Output
915057324
For this arrangement, the expected number of purrs is .