#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
const int M = 2e7 + 5;
long long c[N];
int p[M], vis[M], cnt, n, m, q;
map<long long, long long> phi;
long long qpow(long long x, long long y, long long mo) {
  long long s = 1;
  bool ok = 0;
  if (x >= mo) ok = 1, x %= mo;
  while (y) {
    if (y & 1) s = s * x;
    if (s >= mo) ok = 1, s %= mo;
    x = x * x;
    if (x >= mo) ok = 1, x %= mo;
    y >>= 1;
  }
  return s + ok * mo;
}
long long getphi(long long x) {
  long long tmp = x, p = x;
  for (int i = 2; i * i <= tmp; i++) {
    if (tmp % i == 0) {
      p = p - p / i;
      while (tmp % i == 0) tmp /= i;
    }
  }
  if (tmp > 1) p = p - p / tmp;
  return p;
}
long long dfs(int l, int r, int mo) {
  long long w = c[l];
  if (w == 1 || mo == 1) return 1;
  if (l == r) return w >= mo ? w % mo + mo : w % mo;
  return qpow(w, dfs(l + 1, r, phi[mo]), mo);
}
int main() {
  scanf("%d%d", &n, &m);
  int tmp = m;
  while (tmp > 1) {
    phi[tmp] = getphi(tmp);
    tmp = phi[tmp];
  }
  for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
  scanf("%d", &q);
  int l, r;
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%lld\n", dfs(l, r, m) % m);
  }
  return 0;
}
