#include #include #include using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v.begin(), v.end()) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } const long long MOD = 1000000007; long long GCD(long long a, long long b) { if (b == 0) return a; else return GCD(b, a % b); } // Garner のアルゴリズムの前処理 long long PreGarner(vector &b, vector &m, long long MOD) { long long res = 1; for (int i = 0; i < (int)b.size(); ++i) { for (int j = 0; j < i; ++j) { long long g = GCD(m[i], m[j]); if ((b[i] - b[j]) % g != 0) return -1; m[i] /= g; m[j] /= g; long long gi = GCD(m[i], g); long long gj = g/gi; do { g = GCD(gi, gj); gi *= g, gj /= g; } while (g != 1); m[i] *= gi, m[j] *= gj; b[i] %= m[i], b[j] %= m[j]; } } for (int i = 0; i < (int)b.size(); ++i) { (res *= m[i]) %= MOD; if (m[i] == 1) { m.erase(m.begin() + i); b.erase(b.begin() + i); --i; } } return res; } // 負の数にも対応した mod (a = -11 とかでも OK) inline long long mod(long long a, long long m) { long long res = a % m; if (res < 0) res += m; return res; } // 拡張 Euclid の互除法 long long extGCD(long long a, long long b, long long &p, long long &q) { if (b == 0) { p = 1; q = 0; return a; } long long d = extGCD(b, a%b, q, p); q -= a/b * p; return d; } // 逆元計算 (ここでは a と m が互いに素であることが必要) long long modinv(long long a, long long m) { long long x, y; extGCD(a, m, x, y); return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので } // Garner のアルゴリズム, x%MOD, LCM%MOD を求める (M が互いに素でない場合も対応) // for each step, we solve "coeffs[k] * t[k] + constants[k] = b[k] (mod. m[k])" // coeffs[k] = m[0]m[1]...m[k-1] // constants[k] = t[0] + t[1]m[0] + ... + t[k-1]m[0]m[1]...m[k-2] long long Garner(vector b, vector m, long long MOD) { m.push_back(MOD); // banpei vector coeffs((int)m.size(), 1); vector constants((int)m.size(), 0); for (int k = 0; k < (int)b.size(); ++k) { long long t = mod((b[k] - constants[k]) * modinv(coeffs[k], m[k]), m[k]); for (int i = k+1; i < (int)m.size(); ++i) { (constants[i] += t * coeffs[i]) %= m[i]; (coeffs[i] *= m[k]) %= m[i]; } } return constants.back(); } int main() { int N; cin >> N; vector b(N), m(N); bool exist_non_zero = false; for (int i = 0; i < N; ++i) { cin >> b[i] >> m[i]; if (b[i]) exist_non_zero = true; } long long lcm = PreGarner(b, m, MOD); if (!exist_non_zero) cout << lcm << endl; else if (lcm == -1) cout << -1 << endl; else cout << Garner(b, m, MOD) << endl; }