Tweak the Peak
View as PDFMichael has a favourite kind of skyline: one clean peak, and absolutely no valleys.
You are given an array of distinct integers. You may rearrange its elements in any order.
In a rearranged array, an index is a peak if
and both neighbours are
strictly smaller:
and
.
Similarly, an index is a valley if
and both neighbours are strictly
larger:
and
.
Count the number of distinct rearrangements of the array that have exactly one peak and zero valleys.
Since the answer can be large, output it modulo .
Input
The first line contains the integer , the length of the array.
The second line contains integers:
.
Output
Print the number of distinct rearrangements with exactly one peak and zero valleys, modulo
.
Constraints
- All
are distinct.
Example 1
Input
3
1 2 3
Output
2
Explanation
The valid rearrangements are and
.
Example 2
Input
4
1 2 3 4
Output
6
Example 3
Input
5
10 20 30 40 50
Output
14
Comments