Under construction

Still being built. Things may change

Sum of Circular Sequence Scores

Time limit1sMemory limit1024 MB

Summary
Given counts C_a of each value 1 to N in a circular sequence, sum the product of run lengths over all distinct circular arrangements, modulo 1e9+7.
Level

Hard8 of 10

Topics
Combinatorics, Dynamic programming, Math
Solved
No attempts yet

Problem

Let [h1,h2,⋯ ,hL][h_1, h_2, \cdots, h_L] be a circular integer sequence of length LL. Each hih_i is adjacent to hi+1h_{i+1}, and in particular hLh_L is adjacent to h1h_1. The score of this circular sequence is the product of the sizes of all contiguous segments of equal numbers. For example, if h=[1,1,2,2,1,3,3,3,1,1]h = [1, 1, 2, 2, 1, 3, 3, 3, 1, 1], the score is 4×2×1×3=244 \times 2 \times 1 \times 3 = 24.

Write a program that computes the sum of scores over all distinct circular sequences that contain only integers from 1 to NN, where each integer aa appears exactly CaC_a times. Two circular sequences hh and h′h' are different if there exists an ii with hi≠hi′h_i \ne h'_i.

Input

The first line contains one integer NN (1≤N≤501 \le N \le 50).

The second line contains NN integers C1,C2,⋯ ,CNC_1, C_2, \cdots, C_N (1≤Ca≤1001 \le C_a \le 100), separated by single spaces.

Output

Print, on the first line, the sum of scores over all distinct circular sequences in which each integer aa appears exactly CaC_a times. The sum can be very large, so print it modulo 1 000 000 0071\,000\,000\,007.

Hint

In the first input, the following 66 circular sequences are produced:

  • [1,1,2,2][1, 1, 2, 2]: 44 points
  • [1,2,1,2][1, 2, 1, 2]: 11 point
  • [1,2,2,1][1, 2, 2, 1]: 44 points
  • [2,1,1,2][2, 1, 1, 2]: 44 points
  • [2,1,2,1][2, 1, 2, 1]: 11 point
  • [2,2,1,1][2, 2, 1, 1]: 44 points

The total score is therefore 1818.

Examples5

  1. Example 1

    Input
    2
    2 2
    
    Expected output
    18
    
  2. Example 2

    Input
    3
    4 3 2
    
    Expected output
    7830
    
  3. Example 3

    Input
    4
    4 4 4 4
    
    Expected output
    818559048
    
  4. Example 4

    Input
    5
    1 2 3 4 5
    
    Expected output
    342934740
    
  5. Example 5

    Input
    6
    7 8 9 10 11 12
    
    Expected output
    609539975