Under construction

Still being built. Things may change

Soldiers

Time limit1sMemory limit512 MB

Summary
Count the monotonic lineups of n distinguishable soldiers by height and output the last four digits of the count.
Level

Medium4 of 10

Topics
Combinatorics, Math, Sorting
Solved
No attempts yet

Problem

At the morning roll call in the barracks, every soldier stationed there must line up in a single row. They may not stand in an arbitrary order: they must stand sorted by height, from the tallest soldier to the shortest. The tallest soldier may stand at either end of the row, on the left or on the right. Help them count how many correct line-ups are possible.

Two line-ups are considered the same if and only if every soldier has the same neighbor on their left in both line-ups (or no left neighbor in both) and the same neighbor on their right in both line-ups (or no right neighbor in both).

Write a program that:

  • reads the description of all soldiers in the barracks from standard input,
  • computes the number of ways to line the soldiers up in one row sorted from the tallest to the shortest,
  • prints the last four decimal digits of that number to standard output.

Input

The first line contains one integer nn (1≤n≤200 0001 \le n \le 200\,000), the number of soldiers in the barracks. The second line contains nn integers wiw_i (1≤wi≤1091 \le w_i \le 10^9), separated by single spaces, giving the heights of the soldiers in input order.

Output

Print, on the only line of output, the last four decimal digits of the number of ways to line the soldiers up in one row sorted by height. When that number is at least 10001000, print exactly four digits, padding with leading zeros if necessary. When it is smaller than 10001000, print all of its digits instead.

Hint

For the input with heights 2 3 1 4 4 5 2, all correct line-ups are listed below. Each entry shows a soldier's input index followed by that soldier's height in parentheses.

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

There are 88 of them, which is why the answer for that input is 88.

Examples4

  1. Example 1

    Input
    7
    2 3 1 4 4 5 2
    
    Expected output
    8
    
  2. Example 2

    Input
    1
    5
    
    Expected output
    1
    
  3. Example 3

    Input
    2
    3 5
    
    Expected output
    2
    
  4. Example 4

    Input
    8
    1 1 1 1 1 1 1 1
    
    Expected output
    0320