1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
    Zadanie: Drzewa rozpinające
    Autor: Tomasz Kwiatkowski
*/

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back

using namespace std;
typedef long long ll;

const int MAXN = 5e3 + 7;
const int MOD = 1e9 + 7;

ll invmod[MAXN];

ll pow(int a, int b)
{
    ll res = 1;
    while (b) {
        if (b & 1)
            res = ((ll)res * a) % MOD;
        a = ((ll)a * a) % MOD;
        b /= 2;
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    vector<ll> a(n);
    for (auto& x : a)
        cin >> x;

    for (int i = 1; i <= n; ++i)
        invmod[i] = pow(i, MOD - 2);
    vector<int> dp(1 << n);
    for (int mask = 1; mask < (1 << n); ++mask) {
        int m = __builtin_popcount(mask);
        if (m == 1) {
            dp[mask] = 1;
            continue;
        }
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (((mask >> i) & 1) && ((mask >> j) & 1)) {
                    int m = mask ^ (1 << i) ^ (1 << j);
                    for (int s = m; ; s = (s - 1) & m) {
                        dp[mask] = (dp[mask] + (ll)((ll)(((ll)__gcd(a[i], a[j])*dp[s ^ (1 << i)]) % MOD) * dp[s ^ m ^ (1 << j)]) % MOD) % MOD;
                        if (s == 0) break;
                    }
                }
            }
        }
        dp[mask] = (dp[mask] * (ll)invmod[m - 1]) % MOD;
    }
    cout << dp[(1 << n) - 1] << '\n';
    return 0;
}