#include using namespace std; using ll = long long; /// --- modulo Library {{{ /// ll extgcd(ll a, ll b, ll& x, ll& y) { if(b==0) { x = 1; y = 0; return a; } ll d = extgcd(b, a % b, y, x); y -= (a / b) * x; return d; } ll modpow(ll a, ll b, ll mod = 1e9 + 7) { a = (a % mod + mod) % mod; ll r = 1; while(b){ if(b & 1) r = r * a % mod; a = a * a % mod; b >>= 1; } return r; } ll modinv(ll a, ll mod = 1e9 + 7) { a = (a % mod + mod) % mod; ll x = 0, y = 0; extgcd(a, mod, x, y); return (x % mod + mod) % mod; } /// }}}--- /// // require mods library /// Garner Library {{{ /// ll garner(vector n, vector mods, ll mod){ n.emplace_back(0); mods.emplace_back(mod); vector coeffs(n.size(), 1); // v_i の係数 vector constants(n.size(), 0); // v_i の項より後ろの項の和,答え mod mods[i] for(int i = 0; i < (int) n.size(); i++) { // coeffs[i] * v_i + constants[i] == n[i] (mod mods[i]) を解く ll v = (n[i] - constants[i]) * modinv(coeffs[i], mods[i]) % mods[i]; if (v < 0) v += mods[i]; for (int j = i + 1; j < (int) n.size(); j++) { (constants[j] += coeffs[i] * v) %= mods[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; } } } for(int i = 0; i < n; i++) vs[i] = vs[i] % ms[i]; cout << garner(vs, ms, 1e9 + 7) << endl; }