Entirely Unsorted Sequences#9882

Entirely Unsorted Sequences

1 second
256 megabytes
Hard

You have recently been promoted to lead scientist at NASA, the National Association for Sorting Algorithms. Congratulations! Your main responsibility is testing the sorting algorithms that your team develops. Fortunately, NASA has a large budget this year, and you were able to buy some state-of-the-art integers to test the sorting algorithms with.

As the lead scientist, you are well aware that algorithms are judged by their behaviour on worst-case inputs. So, to test sorting algorithms, you need sequences that are as unsorted as possible.

Given a sequence of numbers (a_1, \ldots, a_n), we say that an element a_k is sorted if a_j \ge a_k for all indices j > k and a_j \le a_k for all indices j < k. For example, in the sequence

(1, 3, 2, 3, 4, 6, 5, 5)

the sorted elements are , the second occurrence of , and . Note that a sequence is sorted if and only if all of its elements are sorted. A sequence is called entirely unsorted if none of its elements is sorted.

Given a sequence of integers, how many entirely unsorted sequences can be obtained by permuting its elements? Two sequences (b_1, \ldots, b_n) and (c_1, \ldots, c_n) are considered different if b_i \ne c_i for some index i \in \{1, \ldots, n\}. Since this number may be very large, output it modulo 10^9 + 9.

Input

The first line contains an integer n~(1 \le n \le 5000).

The second line contains integers a_1, \ldots, a_n~(0 \le a_i \le 10^9).

Output

Print one integer — the number of entirely unsorted sequences that can be obtained by permuting a_1, \ldots, a_n, taken modulo 10^9 + 9.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Example 3

Loading
Loading