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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace std;
 
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
	enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
	ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
	*this << "[";
	for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
	ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vll = vector<ll>;

#ifdef LOCAL
mt19937 rng(69);
#else
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif


const int mod = 1000 * 1000 * 1000 + 7;
const int kMaxVal = 5010;

uniform_int_distribution uniform_mod_p(1, mod - 1);

// Berlekamp-Massey z biblioteczki UW
namespace Massey {
void add_self(int & a, int b) { a += b; if(a >= mod) a -= mod; }
void sub_self(int & a, int b) { a -= b; if(a < 0) a += mod; }
int mul(int a, int b) { return (ll) a * b % mod; }
int my_pow(int a, int b) {
	int r = 1;
	while(b) {
		if(b % 2) r = mul(r, a);
		a = mul(a, a);
		b /= 2;
	}
	return r;
}
int my_inv(int a) { return my_pow(a, mod - 2); }
struct Massey {
	vector<int> start, coef; // 3 optional lines
	vector<vector<int>> powers;
	int memo_inv;
	bool ok;
	// Start here and write the next ~25 lines until "STOP"
	int L; // L == coef.size() <= start.size()
	Massey(vector<int> in) : ok{true} { // O(N^2)
		L = 0;
		const int N = in.size();
		vector<int> C{1}, B{1};
		for(int n = 0; n < N; ++n) {
			assert(0 <= in[n] && in[n] < mod); // invalid input
			B.insert(B.begin(), 0);
			int d = 0;
			for(int i = 0; i <= L; ++i)
				add_self(d, mul(C[i], in[n-i]));
			if(d == 0) continue;
			vector<int> T = C;
			C.resize(max(B.size(), C.size()));
			for(int i = 0; i < (int) B.size(); ++i)
				sub_self(C[i], mul(d, B[i]));
			if(2 * L <= n) {
				L = n + 1 - L;
				B = T;
				d = my_inv(d);
				for(int & x : B) x = mul(x, d);
			}
		}
		//~ cerr << "L = " << L << "\n";
		if (2 * L > N - 2) {
			ok = false;
		}
		// === STOP ===
		for(int i = 1; i < (int) C.size(); ++i)
			coef.push_back((mod - C[i]) % mod);
		assert((int) coef.size() == L);
		for(int i = 0; i < L; ++i)
			start.push_back(in[i]);
		while(!coef.empty() && !coef.back()) { coef.pop_back(); --L; }
		if(!coef.empty()) memo_inv = my_inv(coef.back());
		powers.push_back(coef);
	}
};

} /* namespace Massey */

inline void add_mod(int &a, int b) {
	a += b;
	if (a >= mod) { a -= mod; }
}
inline void sub_mod(int &a, int b) {
	a -= b;
	if (a < 0) { a += mod; }
}
inline int mul_mod(int a, int b) {
	return (int)((ll)a * b % mod);
}
inline int pow_mod(int a, int n) {
	int r = 1;
	while (n) {
		if (n & 1) { r = mul_mod(r, a); }
		n >>= 1;
		a = mul_mod(a, a);
	}
	return r;
}
inline int inv_mod(int a) {
	return pow_mod(a, mod - 2);
}
inline int div_mod(int a, int b) {
	return mul_mod(a, inv_mod(b));
}

int input_n;
int mat_n;
vi elems;
int phi[kMaxVal];
int gcds[kMaxVal][kMaxVal];
vi input_row_sums;

template <typename T>
std::vector<T> BasicSieve(T max_range) {
    std::vector<T> min_prime(max_range + 1);
    std::iota(min_prime.begin(), min_prime.end(), 0);
    for (T p = 2; p * p <= max_range; ++p) {
        if (min_prime[p] != p) { continue; }
        for (T i = p * p; i <= max_range; i += p) {
            if (min_prime[i] == i) {
                min_prime[i] = p;
            }
        }
    }
    return min_prime;
}

void Preproc() {
	auto sieve = BasicSieve(kMaxVal);
	for (int value = 1; value < kMaxVal; ++value) {
		int x = value, y = value;
		while (y > 1) {
			const int p = sieve[y];
			x = (x / p) * (p - 1);
			while (y % p == 0) { y /= p; }
		}
		phi[value] = x;
	}
	
	for (int i = 0; i < kMaxVal; ++i) {
		gcds[0][i] = gcds[i][0] = i;
	}
	for (int i = 1; i < kMaxVal; ++i) {
		for (int j = 1; j < kMaxVal; ++j) {
			if (i >= j) {
				gcds[i][j] = gcds[i - j][i];
			} else {
				gcds[i][j] = gcds[i][j - i];
			}
		}
	}
}

ll x_vec_helper[kMaxVal];
ll y_vec_helper[kMaxVal];

void MultiplyVecByLaplacian(vi &in_vec) {
	memset(x_vec_helper, 0, sizeof(x_vec_helper));
	memset(y_vec_helper, 0, sizeof(y_vec_helper));
	for (int i = 0; i < SZ(in_vec); ++i) {
		x_vec_helper[elems[i]] += in_vec[i];
		x_vec_helper[elems[i]] %= mod;
	}
	for (int d = 1; d < kMaxVal; ++d) {
		ll val = 0;
		for (int i = d; i < kMaxVal; i += d) {
			val += x_vec_helper[i];
		}
		y_vec_helper[d] = (val * phi[d]) % mod;
	}

	memset(x_vec_helper, 0, sizeof(x_vec_helper));
	for (int d = 1; d < kMaxVal; ++d) {
		for (int i = d; i < kMaxVal; i += d) {
			x_vec_helper[i] += y_vec_helper[d];
		}
	}

	for (int i = 0; i < SZ(in_vec); ++i) {
		ll ans = x_vec_helper[elems[i]];
		ans -= (ll)elems[i] * in_vec[i];
		ans = -ans;
		ans += (ll)in_vec[i] * input_row_sums[i];
		ans %= mod;
		if (ans < 0) { ans += mod; }
		in_vec[i] = ans;
		//~ ans[i] = x_vec_helper[elems[i]] % mod;
		//~ sub_mod(ans[i], mul_mod(elems[i], in_vec[i]));
		//~ if (ans[i]) {
			//~ ans[i] = mod - ans[i];
		//~ }
		//~ add_mod(ans[i], mul_mod(in_vec[i], input_row_sums[i]));
	}
	//~ return ans;
}

vi GetRandomVector(int len) {
	vi ans(len);
	for (int i = 0; i < len; ++i) {
		ans[i] = uniform_mod_p(rng);
	}
	return ans;
}

int DotProd(const vi &a, const vi &b) {
	int ans = 0;
	for (int i = 0; i < SZ(a); ++i) {
		add_mod(ans, mul_mod(a[i], b[i]));
	}
	return ans;
}

int LaplacianDeterminant() {
	while (true) {
		const vi coef_vec = GetRandomVector(mat_n);
		const vi row_scale_vec = GetRandomVector(mat_n);
		vi cur_vec = GetRandomVector(mat_n);
		const int num_coefs = mat_n * 2 + 10;
		vi massey_coefs(num_coefs);
		
		for (int i = 0; i < num_coefs; ++i) {
			massey_coefs[i] = DotProd(coef_vec, cur_vec);
			MultiplyVecByLaplacian(cur_vec);
			for (int j = 0; j < mat_n; ++j) {
				cur_vec[j] = mul_mod(cur_vec[j], row_scale_vec[j]);
			}
		}
		
		Massey::Massey massey(massey_coefs);
		if (!massey.ok) { continue; }
		debug() << imie(massey.start) << imie(massey.coef);
		if (SZ(massey.coef) < mat_n) { continue; }
		
		int det = massey.coef.back();
		for (int elem : row_scale_vec) {
			det = div_mod(det, elem);
		}
		if (mat_n % 2 == 0) {
			det = (mod - det) % mod;
		}
		
		return det;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(11);
	cerr << fixed << setprecision(6);
	
	Preproc();
	
	cin >> input_n;
	elems.resize(input_n);
	for (int &x : elems) { cin >> x; }
	
	if (input_n == 1) {
		cout << "1\n";
		return 0;
	}
	
	mat_n = input_n - 1;
	
	input_row_sums.resize(input_n);
	for (int i = 0; i < input_n; ++i) {
		for (int j = 0; j < input_n; ++j) {
			if (i != j) {
				input_row_sums[i] += gcds[elems[i]][elems[j]]; //gcd(elems[i], elems[j]);
			}
		}
	}
	
	cout << LaplacianDeterminant() << "\n";
}