#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_DEPRECATE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair Pii; typedef pair Pll; #define FOR(i,n) for(int i = 0; i < (n); i++) #define sz(c) ((int)(c).size()) #define ten(x) ((int)1e##x) #define tenll(x) ((ll)1e##x) // #pragma comment(linker,"/STACK:36777216") template T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template void chmax(T& l, const T r) { l = max(l, r); } template void chmin(T& l, const T r) { l = min(l, r); } template T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; } template T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; } ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } // x % m[i] == r[i] を満たす,最小の0以上の整数x について, x % mod を求める // ex) // x % 5 == 4 // x % 7 == 1 // x % 11 == 2 // x % 13 = ? // -> x % (5*7*11) = 134 より, 最小のxは 134 // よって, x % 13 = 4 を返す ll garner(vector& mr, int mod){ mr.emplace_back(mod, 0); vector coffs(sz(mr), 1); vector constants(sz(mr), 0); FOR(i, sz(mr) - 1){ // coffs[i] * v + constants[i] == mr[i].second (mod mr[i].first) を解く ll v = (mr[i].second - constants[i]) * mod_inv(coffs[i], mr[i].first) % mr[i].first; if (v < 0) v += mr[i].first; for (int j = i + 1; j < sz(mr); j++) { (constants[j] += coffs[j] * v) %= mr[j].first; (coffs[j] *= mr[i].first) %= mr[j].first; } } return constants[sz(mr) - 1]; } void test(){ vector mr; mr.emplace_back(5, 4); mr.emplace_back(7, 1); mr.emplace_back(11, 2); auto ans = garner(mr, 13); assert(ans == 4); } int solve(vector& v){ const int n = sz(v); FOR(i, n){ for (int j = i + 1; j < n; j++) { int g = gcd(v[i].first, v[j].first); if (g == 1) continue; if (v[i].second % g != v[j].second % g) { return -1; } v[i].first /= g; v[i].second %= v[i].first; } } bool all_zero = true; for (auto mr : v) if (mr.second != 0)all_zero = false; if (all_zero) { ll ans = 1; for (auto mr : v) ans = ans * mr.first % (ten(9) + 7); return (int)ans; } return (int)garner(v, ten(9) + 7); } int main(){ int n; cin >> n; vector v; FOR(i, n){ int x, y; cin >> x >> y; v.emplace_back(y , x); } int ans = solve(v); cout << ans << endl; }