#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template ostream &operator<<(ostream &out, const pair &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template ostream &operator<<(ostream &out, const vector &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 //1e9+7(prime number) #define INF 1000000000 //1e9 #define LLINF 2000000000000000000LL //2e18 #define SIZE 200010 // {a, {b, c}} : a = gcd(p, q) = bp + cq pair> ext_gcd(ll p, ll q) { if (q == 0) return {p, {1, 0}}; auto r = ext_gcd(q, p%q); return {r.first, {r.second.second, r.second.first - p/q*r.second.second}}; } // GCC __gcd(long long A, long long B) ll gcd(ll a, ll b){ if(a == 0) return b; return gcd(b%a, a); } ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } /* Garner */ // modsは互いに素 ll Garner(vector b, vector mods, ll MOD) { mods.push_back(MOD); vector coeffs((int)mods.size(), 1); vector constants((int)mods.size(), 0); for (int i = 0; i < (int)b.size(); i++) { ll m = mods[i]; ll s = (ext_gcd(coeffs[i], m).second.first % m + m) % m; ll t = ((b[i] - constants[i]) * s % m + m) % m; for (int j = i+1; j < (int)mods.size(); j++) { constants[j] = (constants[j] + t * coeffs[j]) % mods[j]; coeffs[j] = (coeffs[j] * m) % mods[j]; } } return constants.back(); } //破壊的 ll preGarner(vector &b, vector &mods, ll MOD) { ll res = 1; for (int i = 0; i < (int)b.size(); i++) { for (int j = 0; j < i; j++) { ll g = gcd(mods[i], mods[j]); if ((b[i] - b[j]) % g != 0) return -1; mods[i] /= g; mods[j] /= g; ll gi = gcd(mods[i], g); ll gj = g / gi; do { g = gcd(gi, gj); gi *= g; gj /= g; } while (g != 1); mods[i] *= gi; mods[j] *= gj; b[i] %= mods[i]; b[j] %= mods[j]; } } for (int i = 0; i < (int)b.size(); ++i) res = res * mods[i] % MOD; return res; } // ll lcm = preGarner(x, y, mod); // if (lcm == -1) //NG; else if (forall x is 0) //lcm or 0 else ans = Garner(x, y); int main(){ int N; vector mods, vals; bool zeroFlag = true; cin >> N; for (int i=0; i> x >> y; vals.push_back(x); mods.push_back(y); zeroFlag &= x == 0; } ll lcm = preGarner(vals, mods, mod); debug(vals); debug(mods); if (lcm == -1) { puts("-1"); } else if (zeroFlag) { printf("%lld\n", lcm); } else { printf("%lld\n", Garner(vals, mods, mod)); } return 0; }