#include int64_t calcGCD(int64_t a, int64_t b) { while (a) { b %= a; std::swap(a, b); } return b; } //////////////////////////// // 拡張ユークリッドの互除法 // //////////////////////////// // x, yは非負整数 // ret[0] * x + ret[1] * y == ret[2] == gcd(x, y) となるようなretを返す std::array extendedEuclidean(const int64_t x, const int64_t y) { if (y == 0) return {1, 0, x}; auto next{extendedEuclidean(y, x % y)}; return {next[1], next[0] - (x / y) * next[1], next[2]}; } ///////////////////////// // Garnerのアルゴリズム // ///////////////////////// int64_t garner(const std::vector& rests, const std::vector& mods, const int64_t ans_mod) { std::vector coefficient(rests); for (int i{}; i < (int)rests.size(); i++) { int64_t mod_multi{1ll}; for (int j{}; j < i; j++) { coefficient[i] = (coefficient[i] + mods[i] - mod_multi * coefficient[j] % mods[i]) % mods[i]; mod_multi = mod_multi * mods[j] % mods[i]; } for (int j{}; j < i; j++) coefficient[i] = coefficient[i] * (extendedEuclidean(mods[j], mods[i])[0] + mods[i]) % mods[i]; } int64_t ret{}, mod_multi{1ll}; for (int i{}; i < (int)rests.size(); i++) { ret = (ret + mod_multi * coefficient[i]) % ans_mod; mod_multi = mod_multi * mods[i] % ans_mod; } return ret; } int64_t solve(); void addMap(std::map>&, int64_t, int, int); int main() { printf("%lld\n", solve()); return 0; } int64_t solve() { constexpr int64_t mod{1'000'000'007}; int N; scanf("%d", &N); std::vector X(N), Y(N); for (int i{}; i < N; i++) scanf("%lld%lld", &X[i], &Y[i]); for (int i{}; i < N; i++) for (int j{}; j < N; j++) { if (i == j) continue; const int64_t gcd{calcGCD(Y[i], Y[j])}; if (X[i] % gcd != X[j] % gcd) return -1; } std::map> primes; for (int i{}; i < N; i++) { int64_t cpy{Y[i]}; for (int64_t j{2}; j * j <= cpy; j++) { if (cpy % j > 0) continue; int count{}; while (cpy % j == 0) { cpy /= j; count++; } addMap(primes, j, count, i); } if (cpy > 1) addMap(primes, cpy, 1, i); } for (auto& e: primes) for (int i{}; i < N; i++) { if (e.second.second == i) continue; while (Y[i] % e.first == 0) Y[i] /= e.first; } for (int i{}; i < N; i++) X[i] %= Y[i]; return garner(X, Y, mod); } void addMap(std::map>& primes, int64_t prime , int count, int index) { auto it{primes.find(prime)}; if (it == primes.end()) primes[prime] = {count, index}; else if (count > it->second.first) it->second = {count, index}; }