This page is still under construction.

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

Counting Functions

Time limit1sMemory limit32 MB

Summary
Count functions f on {1..N} such that each i returns to itself after exactly A_i iterations, for N up to 16.
Level

Hard8 of 10

Topics
Combinatorics, Graph, Bit manipulation
Solved
No attempts yet

Problem

Minhyuk built a function f:S→Sf : S \to S from the set S={1,2,…,N}S = \{1, 2, \dots, N\} to itself. Write fkf^k for applying ff exactly kk times in a row. The function Minhyuk built satisfies the following.

  • fA1(1)=1f^{A_1}(1) = 1
  • fA2(2)=2f^{A_2}(2) = 2
  • …\dots
  • fAN(N)=Nf^{A_N}(N) = N

Minhyuk wants to know how many different functions satisfy this. Given A1,A2,…,ANA_1, A_2, \dots, A_N, write a program that counts them. Two functions gg and hh are different when there is at least one xx with g(x)≠h(x)g(x) \neq h(x).

Input

The first line contains the size of the domain, NN. (3≤N≤163 \le N \le 16)

The second line contains the positive integers A1,A2,…,ANA_1, A_2, \dots, A_N, separated by spaces. (1≤Ai≤1,000,0001 \le A_i \le 1{,}000{,}000)

Output

Print the number of functions that satisfy the condition on the first line.

Examples2

  1. Example 1

    Input
    4
    3 6 9 12
    
    Expected output
    10
    
  2. Example 2

    Input
    16
    720720 720720 720720 720720 720720 720720 720720 720720 720720 720720 720720 720720 720720 720720 720720 720720
    
    Expected output
    20922789888000