#include using namespace std; using uint = unsigned int; using lint = long long int; using ulint = unsigned long long int; template using V = vector; template using VV = V< V >; template void assign(V& v, int n, const U& a) { v.assign(n, a); } template void assign(V& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); } lint tmod(lint a, lint p) { assert(p > 0); return (a %= p) < 0 ? a + p : a; } lint mod_inv(lint a, lint p) { a = tmod(a, p); lint b = p, x = 1, u = 0; while (b) { lint q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); } return a == 1 ? tmod(x, p) : -1; } bool pre(V& a, V& p) { int n = a.size(); assert(p.size() == n); for (int i = 0; i < n; ++i) { a[i] = tmod(a[i], p[i]); } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { lint d = __gcd(p[i], p[j]); if (a[i] % d != a[j] % d) return false; p[i] /= d; p[j] /= d; while (true) { lint e = __gcd(d, p[j]); if (e == 1) break; p[j] *= e; d /= e; } p[i] *= d; a[i] %= p[i]; a[j] %= p[j]; } return true; } lint CRT(const V& a, const V& p) { int n = a.size(); lint x = 0; V y(n); lint prod = 1; for (int i = 0; i < n; ++i) { y[i] = tmod(a[i] - x, p[i]); for (int j = 0; j < i; ++j) { (y[i] *= mod_inv(p[j], p[i])) %= p[i]; } x += prod * y[i]; prod *= p[i]; } return x; } lint CRT(const V& a, const V& p, lint mod) { int n = a.size(); V y(n); for (int i = 0; i < n; ++i) { y[i] = a[i]; lint prod = 1; for (int j = 0; j < i; ++j) { y[i] -= prod * y[j] % p[i]; (prod *= p[j]) %= p[i]; } y[i] = tmod(y[i], p[i]); for (int j = 0; j < i; ++j) { (y[i] *= mod_inv(p[j], p[i])) %= p[i]; } } lint res = 0, prod = 1; for (int i = 0; i < n; ++i) { res += prod * y[i] % mod; (prod *= p[i]) %= mod; } return res % mod; } constexpr lint mod = 1e9 + 7; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; V a(n), p(n); for (int i = 0; i < n; ++i) cin >> a[i] >> p[i]; if (!pre(a, p)) return cout << -1 << '\n', 0; if (all_of(begin(a), end(a), [](lint e) { return e == 0; })) { cout << accumulate(begin(p), end(p), 1LL, [&](lint acc, lint e) { return acc * e % mod; }) << '\n'; return 0; } lint res = CRT(a, p, mod); cout << res << '\n'; }