from math import gcd import sys sys.setrecursionlimit(10**6) def extgcd(a, b, x1=1, y1=0, x2=0, y2=1): if b == 0: return x1, y1 q = a//b return extgcd(b, a%b, x2 ,y2, x1-x2*q, y1-y2*q) def crt(a, m, md): a = a[:] m = m + [md] N = len(m) M = [[1]*N for i in range(N)] R = [0]*N for i in range(N): for j in range(1, i+1): M[i][j] = (M[i][j-1] * m[j-1]) % m[i] R[i] = extgcd(M[i][i], m[i])[0] % m[i] memo = [[-1]*N for i in range(N)] def rec(i, j): if i == -1: return 0 if memo[i][j] >= 0: return memo[i][j] v = (rec(i-1, j) + M[j][i]*(R[i]*(a[i]-rec(i-1, i))%m[i])) % m[j] memo[i][j] = v return memo[i][j] rec(N-2, N-1) return memo[N-2][N-1], M[N-1][N-1] def pre_garner(a, m): for i in range(len(m)): for j in range(len(m)): if i == j: continue g = gcd(m[i], m[j]) if (a[i]-a[j])%g != 0: return False m[i] //= g m[j] //= g gi = gcd(g, m[i]) gj = g//gi while g != 1: g = gcd(gi, gj) gi *= g gj //= g m[i] *= gi m[j] *= gj a[i] %= m[i] a[j] %= m[j] return True n = int(input()) a, m = zip(*(tuple(map(int, input().split())) for _ in range(n))) a, m = list(a), list(m) if not pre_garner(a, m): print(-1) exit() x = crt(a, m, 10**9+7) x, mm = x if all(ai==0 for ai in a): print(mm) else: print(x)