Task
An array of numbers is said to be W-shaped if it meets the following conditions:
- It consists of four segments in decreasing order, increasing order, decreasing order, increasing order.
- The ordering is not strict, so increasing and decreasing segments may include consecutive equal elements.
- Every two consecutive segments have a common endpoint.
- Every segment contains at least two distinct values.
For example, the array is W-shaped, since it consists of the segments . The array is not W-shaped. It could be broken into the segments , however the segment does not contain two distinct values.
Given an array of integers, how many distinct permutations of the array are W-shaped? Two permutations of the array, and , are considered distinct if there exists a position where . In the example above, should only be counted once, because permuting the three ’s does not create distinct permutations.
Input data
The first line contains . The second line contains the values of the array, separated by spaces.
Output data
Print a single number: the number of distinct W-shaped permutations, modulo .
Constraints and clarifications
- Array values are integers between and inclusive.
- Test cases will be scored individually.
# | Points | Constraints |
---|---|---|
1 | 20 | There are only two distinct values among the elements. |
2 | 30 | All the elements have distinct values. |
3 | 50 | No additional constraints. |
Example 1
stdin
5
3 1 4 2 3
stdout
6
Explanation
The six distinct W-shaped permutations are:
Example 2
stdin
7
1 2 2 2 3 4 4
stdout
72