Balanced scales V45017


Statement
 

pdf   zip

Given nn weights, we have to place all of them on a scale, one after another, in such a way that the right pan is never heavier than the left pan. Please compute the number of ways of doing this.

For example, for n=3n=3 and weights {1,2,4}\{1,2,4\}, possible solutions are

(1,2,4),(2,1,4),(2,4,1r),(2,1r,4),(4,1r,2r),(1\ell,2\ell,4\ell),(2\ell,1\ell,4\ell), (2\ell,4\ell,1r), (2\ell,1r,4\ell), (4\ell,1r,2r), \cdots

where 11\ell means that the weight 11 is placed on the left pan and 2r2r means that the weight 22 is placed on the right pan. We remark, as it can be seen in the example, that the order in which we place the weights does matter. Hence, (2,4,1r)(2\ell,4\ell,1r) and (2,1r,4)(2\ell,1r,4\ell) are different solutions.

Input

Input consists of several cases, each with the number of weights nn followed by nn different weights, all between 1 and 10610^6. Assume 1n81 \le n \le 8.

Output

For every case, print the number of correct ways of placing the weights on the scale. This number will never be larger than 10710^7.

Public test cases
  • Input

    1   20
    3   1 2 4
    3   6 10 4
    8   1 2 3 4 5 6 7 8
    

    Output

    1
    15
    17
    2130717
    
  • Information
    Author
    Albert Oliveras
    Language
    English
    Translator
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++