Consecutive Neighbours
View as PDF
Submit solution
Points:
100
Time limit:
1.0s
PyPy 3
2.0s
Python 3
2.0s
Memory limit:
250M
Problem type
A uniformly random permutation of
is generated. For each
, if the values
and
appear in adjacent positions of
, you score
.
Let be the total score. Output the expected value of
.
It can be shown that the answer can be expressed as a rational number in lowest
terms with
coprime to
. Output
.
Input
The first line contains an integer .
The second line contains integers
. If
, this
line is empty.
Output
Print a single integer: the expected total score modulo .
Constraints
Example 1
Input
3
1 1
Output
333333337
Explanation
There are two pairs, and
. Each pair of consecutive values occupies
adjacent positions with probability
, so the expected score is
.
.
Example 2
Input
2
10
Output
10
Explanation
The two values are always adjacent, so the score is always .
Example 3
Input
1
Output
0
Explanation
There are no consecutive pairs when .
Comments