AlgoLensArena
ContestlərProblemlərOtaqlar
Vizualizator
← Məsələlər

BAPC18-093.Entirely Unsorted Sequences

Medium · 1 s · 256 MB

  • icpc
  • bapc
  • 2018

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

As the lead scientist, you are well aware that algorithms are tested 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 (a1,…,an)(a_1,\ldots,a_n)(a1​,…,an​) we say that an element aka_kak​ is sorted if for all indices jjj such that j>kj > kj>k, aj≥aka_j \geq a_kaj​≥ak​ and for all indices jjj such that j<kj < kj<k, aj≤aka_j \leq a_kaj​≤ak​. For example, in

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

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

Given a sequence of integers, what is the number of entirely unsorted sequences you can make by permuting its elements? Two sequences (b1,…,bn)(b_1, \dots, b_n)(b1​,…,bn​) and (c1,…,cn)(c_1, \dots, c_n)(c1​,…,cn​) are considered to be different if there is some index i∈{1,…,n}i \in \left\{1, \dots, n \right\}i∈{1,…,n} for which bi≠cib_i \neq c_ibi​=ci​. Because the number of permutations may be very large, please give it modulo 109+910^9 + 9109+9.

Giriş formatı

The input starts with an integer 1≤n≤50001 \leq n \leq 50001≤n≤5000. Then follows a single line with nnn integers a1,…,ana_1, \ldots, a_na1​,…,an​, with 0≤ai≤1090 \leq a_i \leq 10^90≤ai​≤109 for all iii.

Çıxış formatı

Print a single integer: the number of entirely unsorted sequences you can make by permuting the aia_iai​, modulo 109+910^9 + 9109+9.

Qeyd

Source: Benelux Algorithm Programming Contest 2018 · Author: <author name> · License: CC BY-SA 4.0 · Original: https://bapc.eu · Statement adapted for AlgoLens and shared under the same license.

Nümunələr

Giriş #1

4
0 1 2 3

Çıxış #1

14

Giriş #2

5
1 1 2 1 1

Çıxış #2

1

Giriş #3

13
1 2 3 4 5 6 7 8 9 10 11 12 13

Çıxış #3

298600727