import java.util.Arrays; import java.util.Scanner; class Scratch { public static long mod_inv(long a, long m){ return (a == 1 ? 1 : (1 - m*mod_inv(m%a, a)) / a + m); } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a % b); } public static boolean make_so(long[] as, long[] ms){ while(true) { boolean updated = false; for (int fst = 0; fst < ms.length; fst++) { for (int snd = fst + 1; snd < ms.length; snd++) { long gcd = gcd(ms[fst], ms[snd]); if (gcd == 1) { continue; } updated = true; if (as[fst] % gcd != as[snd] % gcd) { return false; } ms[fst] /= gcd; ms[snd] /= gcd; while (true) { long gt = gcd(ms[fst], gcd); if (gt == 1) { break; } ms[fst] *= gt; gcd /= gt; } ms[snd] *= gcd; as[fst] %= ms[fst]; as[snd] %= ms[snd]; } } if(!updated){ break; } } return true; } public static long chinese_remainder(long[] as, long[] ms){ long[] vs = new long[ms.length]; long[] sums = new long[ms.length]; long[] invs = new long[ms.length]; for(int i = 0; i < ms.length; i++){ invs[i] = 1; // mod(ms[i]) の中で 1 / (ms[0] * ms[1] ... ms[i - 1]) を求める sums[i] = as[i]; // mod(ms[i]) の中で, a[i] - v[0] - v[1]ms[0] ... を求める long mult = 1; // mod(ms[i]) の中で, ms[0] * ... * ms[i - 2] を求める for(int j = 0; j < i; j++){ sums[i] -= (vs[j] * mult) % ms[i]; if(sums[i] < 0){ sums[i] += ms[i]; } invs[i] = (invs[i] * mod_inv(ms[j], ms[i])) % ms[i]; mult = (mult * ms[j]) % ms[i]; } vs[i] = (sums[i] * invs[i]) % ms[i]; } boolean flg = true; for(int i = 0; i < ms.length; i++){ if(vs[i] != 0){ flg = false; break;} } if(flg){ long prod = 1; for(final long p : ms){ prod *= p; prod %= 1000000007; } return prod; } long ret = 0; for(int i = 0; i < ms.length; i++){ long mult = 1; for(int j = 0; j < i; j++){ mult *= ms[j]; mult %= 1000000007;}// ここで MOD が必要な場合は取る ret += vs[i] * mult;// ここで MOD が必要な場合は取る ret %= 1000000007; } return ret; } public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { final int N = sc.nextInt(); long[] as = new long[N]; long[] ms = new long[N]; for (int i = 0; i < N; i++){ as[i] = sc.nextLong(); ms[i] = sc.nextLong(); } final boolean so = make_so(as, ms); //System.out.println(Arrays.toString(as)); //System.out.println(Arrays.toString(ms)); if(!so){ System.out.println(-1); return; } long prod = 1; for(final long p : ms){ prod *= p; prod %= 1000000007; } final long ret = chinese_remainder(as, ms); System.out.println(ret); } } }