Geometric progression with the first element a and common ratio b is a sequence of numbers a, ab, ab2, ab3, ....

You are given n integer geometric progressions. Your task is to find the smallest integer x, that is the element of all the given progressions, or else state that such integer does not exist.

Input

The first line contains integer (1 ≤ n ≤ 100) — the number of geometric progressions. 

Next n lines contain pairs of integers a, b (1 ≤ a, b ≤ 109), that are the first element and the common ratio of the corresponding geometric progression.

Output

If the intersection of all progressions is empty, then print  - 1, otherwise print the remainder of the minimal positive integer number belonging to all progressions modulo 1000000007 (109 + 7).

Examples

Input

2
2 2
4 1


Output

4


Input

2
2 2
3 3


Output

-1

Note

In the second sample test one of the progressions contains only powers of two, the other one contains only powers of three.
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
long long mult(long long a, long long b, long long mod) {
  return (a * b) % mod;
}
long long pw(long long a, long long b) {
  if (b == 0) return 1;
  if (b & 1) return mult(a, pw(a, b - 1), mod);
  long long res = pw(a, b / 2);
  return mult(res, res, mod);
}
long long inv(long long a, long long b) {
  if (a == 0) return 0;
  if (a == 1) return 1;
  if (a >= b) return inv(a % b, b);
  return mult(b - inv(b % a, b), b / a, b);
}
long long gcd(long long a, long long b) {
  while (a > 0 && b > 0) {
    if (a < b) swap(a, b);
    a %= b;
  }
  return a + b;
}
void no() {
  cout << -1;
  exit(0);
}
const int maxN = 105;
int n, k;
int a[maxN], b[maxN], copa[maxN], copb[maxN];
vector<int> primes;
vector<int> valA[maxN];
vector<int> valB[maxN];
vector<bool> skip;
vector<int> nvalA[maxN], nvalB[maxN];
void go(int p) {
  for (int i = 1; i <= n; i++) {
    int exp = 0;
    while (a[i] % p == 0) {
      a[i] /= p;
      exp++;
    }
    valA[i].push_back(exp);
    exp = 0;
    while (b[i] % p == 0) {
      b[i] /= p;
      exp++;
    }
    valB[i].push_back(exp);
  }
}
void can(int ind, long long t) {
  vector<long long> vals;
  vals.resize(k);
  for (int i = 0; i < k; i++) {
    vals[i] = t * valB[ind][i] + valA[ind][i];
  }
  for (int i = 1; i <= n; i++) {
    int t = -1;
    for (int j = 0; j < k; j++) {
      if (t != -1) {
        if (valA[i][j] + t * valB[i][j] != vals[j]) no();
      } else {
        if (valB[i][j] == 0) {
          if (valA[i][j] != vals[j]) no();
        } else {
          if (valA[i][j] > vals[j]) no();
          if ((vals[j] - valA[i][j]) % valB[i][j] != 0) no();
          t = (vals[j] - valA[i][j]) / valB[i][j];
        }
      }
    }
  }
  cout << mult(copa[ind], pw(copb[ind], t), mod);
  exit(0);
}
pair<long long, long long> unite(pair<long long, long long> a,
                                 pair<long long, long long> b) {
  long long a1 = a.first;
  long long a2 = b.first;
  long long b1 = a.second;
  long long b2 = b.second;
  long long dif = a2 - a1;
  long long cop = b1;
  dif = (dif % b2 + b2) % b2;
  long long d = gcd(b1, b2);
  if (dif % d != 0) {
    no();
  }
  b1 /= d;
  dif /= d;
  b2 /= d;
  long long x = mult(dif, inv(b1, b2), b2);
  long long nx = x * cop + a1;
  return make_pair(nx, cop * b2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    copa[i] = a[i];
    copb[i] = b[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 2; 1LL * j * j <= a[i]; j++) {
      if (a[i] % j == 0) {
        go(j);
      }
    }
    if (a[i] > 1) go(a[i]);
    for (int j = 2; 1LL * j * j <= b[i]; j++) {
      if (b[i] % j == 0) {
        go(j);
      }
    }
    if (b[i] > 1) go(b[i]);
  }
  k = valA[1].size();
  skip.resize(k, false);
  for (int i = 0; i < k; i++) {
    int zero = -1;
    int nonzero = -1;
    for (int j = 1; j <= n; j++) {
      if (valB[j][i] == 0) zero = j;
      if (valB[j][i] != 0) nonzero = j;
    }
    if (nonzero == -1) {
      for (int j = 1; j <= n; j++) {
        if (valA[j][i] != valA[1][i]) no();
      }
      skip[i] = true;
    } else if (zero != -1) {
      int finalval = valA[zero][i];
      finalval -= valA[nonzero][i];
      if (finalval < 0) no();
      if (finalval % valB[nonzero][i] != 0) no();
      int t = finalval / valB[nonzero][i];
      can(nonzero, t);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (skip[j]) continue;
      nvalA[i].push_back(valA[i][j]);
      nvalB[i].push_back(valB[i][j]);
    }
    valA[i] = nvalA[i];
    valB[i] = nvalB[i];
  }
  if (valA[1].size() == 0) {
    cout << copa[1];
    exit(0);
  }
  k = valA[1].size();
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < k; j++) {
      if (valB[1][0] * valB[i][j] != valB[1][j] * valB[i][0]) {
        int det1 = -valB[1][0] * valB[i][j] + valB[1][j] * valB[i][0];
        int det2 = +(valA[i][j] - valA[1][j]) * valB[i][0] -
                   valB[i][j] * (valA[i][0] - valA[1][0]);
        int det3 = +(valA[i][j] - valA[1][j]) * valB[1][0] -
                   valB[1][j] * (valA[i][0] - valA[1][0]);
        int t1 = det2 / det1;
        if (det1 * t1 != det2) no();
        if (t1 < 0) no();
        can(1, t1);
      }
      if ((valB[1][0] * (valA[i][j] - valA[1][j]) -
           valB[1][j] * (valA[i][0] - valA[1][0])) != 0)
        no();
    }
  }
  vector<pair<int, int> > conditions;
  long long limit = 0;
  for (int i = 2; i <= n; i++) {
    int x = valB[1][0];
    int y = valA[i][0] - valA[1][0];
    int mod = valB[i][0];
    if (y >= 0) {
      limit = max(limit, (long long)(y + x - 1) / x);
    }
    y = (y % mod + mod) % mod;
    int d = gcd(x, mod);
    if (y % d != 0) no();
    x /= d;
    mod /= d;
    y /= d;
    if (mod == 1)
      conditions.push_back(make_pair(0, 1));
    else {
      int val = mult(y, inv(x, mod), mod);
      conditions.push_back(make_pair(val, mod));
    }
  }
  if (conditions.size() == 0) {
    can(1, 0);
  }
  pair<long long, long long> cur = make_pair((long long)conditions[0].first,
                                             (long long)conditions[0].second);
  for (int i = 1; i < conditions.size(); i++) {
    cur = unite(cur, conditions[i]);
  }
  long long t = cur.first;
  if (cur.second == 1) t = limit;
  while (t < limit) t += cur.second;
  can(1, t);
}
