This page is still under construction.

Parts of this page are still being built. What you see may change.

Road Network 2

Time limit5sMemory limit128 MB

Summary
Count the labeled trees that realize a prescribed degree sequence, or report that none exist, with n up to two million.
Level

Medium6 of 10

Topics
Tree, Combinatorics, Math
Solved
No attempts yet

Problem

Byteland has nn cities, numbered 11 through nn. Every road is bidirectional and connects two different cities. Between any two different cities there is exactly one path of roads that visits no city more than once. In other words, the road network is a tree with nn vertices and n−1n-1 edges.

We want to build a road network in which exactly did_i roads meet at city ii (that is, city ii has degree did_i). Many different networks may satisfy these conditions. Determine how many different road networks satisfy them. Cities carry distinct labels, so two networks are considered different whenever their sets of roads differ.

Input

The first line contains an integer nn (2≤n≤20000002 \le n \le 2000000). The second line contains nn integers d1,d2,…,dnd_1, d_2, \ldots, d_n (1≤di≤n−11 \le d_i \le n-1) separated by spaces, where did_i is the degree of city ii.

Output

If no road network satisfies the conditions, print BRAK (Polish for 'none') on the only line. Otherwise, print the number of different road networks that satisfy the conditions, modulo 1,000,000,007.

Hint

Examples4

  1. Example 1

    Input
    6
    1 1 3 3 1 1
    
    Expected output
    6
    
  2. Example 2

    Input
    2
    1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    3
    1 1 1
    
    Expected output
    BRAK
    
  4. Example 4

    Input
    3
    2 1 1
    
    Expected output
    1