#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
const int MOD = 1e9 + 7;
int n, q, sz, cnt[MAX + 1], nt[MAX + 1], listPrime[MAX + 1], cntPrime;
long long a, b, mul[MAX + 1], res[MAX + 1], lta[MAX];
bool isPrime[MAX + 1];
vector<int> adj[MAX + 1];
struct Node {
  int lef, rig, id;
};
Node que[MAX + 1];
long long lt(long long a, int b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    b >>= 1;
    a = (a * a) % MOD;
  }
  return res;
}
bool cmp(const Node& x, const Node& y) {
  if (x.lef / sz != y.lef / sz) return x.lef / sz < y.lef / sz;
  return x.rig < y.rig;
}
int main() {
  ios_base::sync_with_stdio(0);
  isPrime[1] = true;
  for (int i = 2; i * i <= MAX; ++i)
    if (!isPrime[i]) {
      nt[i] = i;
      for (int j = 2; j <= MAX / i; ++j) {
        isPrime[i * j] = true;
        nt[i * j] = i;
      }
    }
  for (int i = 1; i <= MAX; ++i) {
    if (!isPrime[i]) {
      listPrime[++cntPrime] = i;
      nt[i] = i;
    }
    lta[i] = lt(i, MOD - 2);
  }
  cin >> n;
  mul[0] = 1;
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    mul[i] = (mul[i - 1] * a) % MOD;
    while (a > 1) {
      adj[i].push_back(b = nt[a]);
      while (a > 1 && a % b == 0) a /= b;
    }
  }
  cin >> q;
  sz = sqrt(n);
  if (sz == 0) ++sz;
  for (int i = 1; i <= q; ++i) {
    cin >> que[i].lef >> que[i].rig;
    que[i].id = i;
  }
  sort(que + 1, que + 1 + q, cmp);
  for (int i = 1; i <= q; ++i) {
    res[que[i].id] = (mul[que[i].rig] * lt(mul[que[i].lef - 1], MOD - 2)) % MOD;
    if (i == 1 || que[i].lef / sz > que[i - 1].lef / sz) {
      for (int j = 1; j <= cntPrime; ++j) cnt[listPrime[j]] = 0;
      a = b = 1;
      for (int j = que[i].lef; j <= que[i].rig; ++j)
        for (int k = 0; k < adj[j].size(); ++k)
          if (++cnt[adj[j][k]] == 1) {
            a = (a * adj[j][k]) % MOD;
            b = (b * (adj[j][k] - 1)) % MOD;
          }
    } else {
      for (int j = que[i - 1].rig + 1; j <= que[i].rig; ++j)
        for (int k = 0; k < adj[j].size(); ++k)
          if (++cnt[adj[j][k]] == 1) {
            a = (a * adj[j][k]) % MOD;
            b = (b * (adj[j][k] - 1)) % MOD;
          }
      if (que[i - 1].lef < que[i].lef) {
        for (int j = que[i - 1].lef; j < que[i].lef; ++j)
          for (int k = 0; k < adj[j].size(); ++k)
            if (--cnt[adj[j][k]] == 0) {
              a = (a * lta[adj[j][k]]) % MOD;
              b = (b * lta[adj[j][k] - 1]) % MOD;
            }
      } else {
        for (int j = que[i - 1].lef - 1; j >= que[i].lef; --j)
          for (int k = 0; k < adj[j].size(); ++k)
            if (++cnt[adj[j][k]] == 1) {
              a = (a * adj[j][k]) % MOD;
              b = (b * (adj[j][k] - 1)) % MOD;
            }
      }
    }
    res[que[i].id] = (res[que[i].id] * lt(a, MOD - 2)) % MOD;
    res[que[i].id] = (res[que[i].id] * b) % MOD;
  }
  for (int i = 1; i <= q; ++i) cout << res[i] << endl;
  return 0;
}
