// includes {{{ #include #include #include #include #include #include #include #include #include #include #include #include #include // }}} using namespace std; using ll = long long; /// --- math {{{ /// ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll extgcd(ll a, ll b, ll &x, ll &y) { ll d; return b == 0 ? (x = 1, y = 0, a) : (d = extgcd(b, a % b, y, x), y -= a / b * x, d); } ll modinv(ll a, ll mod = 1e9 + 7) { ll x = 0, y = 0; extgcd(a, mod, x, y); return (x + mod) % mod; } ll modpow(ll a, ll b, ll mod = 1e9 + 7) { ll r = 1; a %= mod; while(b) { if(b & 1) r = r * a % mod; a = a * a % mod; b >>= 1; } return r; } /// }}}--- /// // require math library /// --- Garner Library {{{ /// ll garner(const vector< ll > &x, vector< ll > mods, ll mod) { mods.emplace_back(mod); vector< ll > coeffs(x.size() + 1, 1); // coeffs[i]v_i vector< ll > constants(x.size() + 1, 0); for(size_t i = 0; i < x.size(); i++) { // x[i] - constants[i] == coeffs[i] * v_i (mod mods[i]) ll v = (x[i] - constants[i]) * modinv(coeffs[i], mods[i]) % mods[i]; if(v < 0) v += mods[i]; for(size_t j = i + 1; j < x.size() + 1; j++) { // coeffs[j] is (mod mods[j]) constants[j] = (constants[j] + coeffs[j] * v) % mods[j]; coeffs[j] = (coeffs[j] * mods[i]) % mods[j]; } } return constants.back(); } /// }}}--- /// int main() { ios::sync_with_stdio(false), cin.tie(0); int n; cin >> n; vector vs(n), ms(n); for(int i = 0; i < n; i++) { cin >> vs[i] >> ms[i]; } vector primes; for(int i = 2; (ll) i*i <= 1e9; i++) { int flag = 1; for(int x : primes) if(i % x == 0) { flag = 0; break; } if(flag) primes.emplace_back(i); } for(int m : ms) { for(int p : primes) while(m % p == 0) m /= p; if(m != 1) primes.emplace_back(m); } for(int p : primes) { vector< pair > ps; for(int i = 0; i < n; i++) { if(ms[i] % p == 0) { int lg = 0; while(ms[i] % p == 0) ms[i] /= p, lg++; ps.emplace_back(lg, i); } } if(ps.size() < 1) continue; sort(begin(ps), end(ps)); int base = vs[ps.back().second]; for(auto d : ps) { if(vs[d.second] % p != base % p) { cout << -1 << endl; return 0; } if(d.second == ps.back().second) { for(int i = 0; i < d.first; i++) ms[d.second] *= p; } } } int allzero = 1; ll ans = 1; for(int i = 0; i < n; i++) { vs[i] = vs[i] % ms[i]; if(vs[i] != 0) allzero = 0; ans = ans * ms[i] % int(1e9 + 7); } if(allzero) { cout << ans << endl; return 0; } cout << garner(vs, ms, 1e9 + 7) << endl; }