cho.sh
Notes
Loading...

Bead Necklace

Time limit

2s

Memory limit

128 MB

Problem

Dasom wants to make a straight necklace with beads of N colors. Beads of the same type have the same color, and beads of different types have different colors. For every three consecutive beads in the necklace, all three colors must be distinct.

For example, suppose there are two beads of type 1, one bead of type 2, and one bead of type 3. If type 1 is green, type 2 is blue, and type 3 is red, then the two green beads must be placed at the two ends. The two possible necklaces are green-red-blue-green and green-blue-red-green.

All available beads must be used. The necklace is a straight line, not a circle, so the first bead and the last bead are not adjacent.

Compute the number of necklaces that can be made.

Input

The first line contains the number of bead types N. N is between 3 and 5, inclusive. Each of the next N lines contains the count of one bead type, in order from type 1 through type N. Each count is a positive integer not greater than 10, and the total number of beads is at most 35.

Output

Print the number of necklaces that can be made.