#include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define elif else if #define sp(x) fixed << setprecision(x) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using ll = long long; using pii = pair; using pil = pair; using pli = pair; using pll = pair; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; const double pi = acos(-1.0); const double EPS = 1e-10; template bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; //ユークリッドの互除法を用いた計算 //計算量 O(log(max(A, B))) template T gcd(const T &a, const T &b){ if(b == 0) return a; else return gcd(b, a%b); } template T lcm(const T &a, const T &b) {return a*(b/gcd(a,b));} template T extgcd(const T &a, const T &b, T &x, T &y){ if(b == 0) {x = 1, y = 0; return a;} T g = extgcd(b, a%b, y, x); y -= (a/b)*x; return g; } int mod(const ll &a, const int &m){ int ret = a%m; return ret+(ret < 0? m : 0); } int modinv(const int &a, const int &m){ //aとmは互いに素 int x, y; extgcd(a, m, x, y); return mod(x, m); } template T floor_sum(const T &n, const T &m, T a, T b){ //Σ(floor((a*i+b)/m)) (0<=i pair Chinese_reminder_theorem(const T &a1, const T &m1, const T &a2, const T &m2){ T x, y, g = extgcd(m1, m2, x, y); if((a2-a1)%g != 0) return make_pair(0, -1); T m = m1*(m2/g); T tmp = mod(x*((a2-a1)/g), m2/g); T a = (m1*tmp+a1) % m; return make_pair(a, m); } bool prepare_Garner(vector &a, vector &m){ int n = sz(a); rep(i, n){ rep(j, i){ int g = gcd(m[i], m[j]); if((a[i]-a[j])%g != 0) return false; m[i] /= g, m[j] /= g; int gi = gcd(m[i], g), gj = g/gi; do{ g = gcd(gi, gj); gi *= g, gj /= g; } while(g > 1); m[i] *= gi, m[j] *= gj; } } return true; } int Garner(vector a, vector m, const int &M){ //mの各要素はそれぞれ互いに素 m.pb(M); vector coeffs(sz(m), 1); vector constants(sz(m), 0); rep(k, sz(a)){ ll x = a[k]-constants[k], y = modinv(coeffs[k], m[k]); ll t = mod(x*y, m[k]); rep2(i, k+1, sz(m)-1){ constants[i] += t*coeffs[i], constants[i] %= m[i]; coeffs[i] *= m[k], coeffs[i] %= m[i]; } } return constants.back(); } //https://yukicoder.me/problems/no/187 int main(){ int N; cin >> N; vector a(N), m(N); rep(i, N) cin >> a[i] >> m[i]; if(!prepare_Garner(a, m)) {cout << -1 << endl; return 0;} ll l = 1; rep(i, N) l *= m[i], l %= MOD; bool flag = true; rep(i, N) if(a[i] != 0) flag = false; if(flag) {cout << l << endl; return 0;} cout << Garner(a, m, MOD) << endl; }