atcoder#ABC209F. [ABC209F] Deforestation
[ABC209F] Deforestation
Score : points
Problem Statement
There are trees standing in a row from left to right. The -th tree from the left, Tree , has the height of .
You will now cut down all these trees in some order you like. Formally, you will choose a permutation of and do the operation below for each in this order.
- Cut down Tree , that is, set to , at a cost of .
Here, we assume .
In other words, the cost of cutting down a tree is the total height of the tree and the neighboring trees just before doing so.
Find the number of permutations that minimize the total cost of cutting down the trees. Since the count may be enormous, print it modulo .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of permutations , modulo , that minimize the total cost of cutting down the trees.
3
4 2 4
2
There are two permutations that minimize the total cost: and .
Below, we will show the process of cutting down the trees for , for example.
- First, Tree is cut down at a cost of .
- Next, Tree is cut down at a cost of .
- Finally, Tree is cut down at a cost of .
The total cost incurred is .
3
100 100 100
6
15
804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422 25202363 350490028 783368691 102520060 44897764
54537651
Be sure to print the count modulo .