Balanced scales

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.

Problem information

Author: Albert Oliveras

Generation: 2026-01-25T13:09:53.110Z

© Jutge.org, 2006–2026.
https://jutge.org