#include <bits/stdc++.h>
using namespace std;
long long arr[100005];
map<long long, long long> phi;
long long count_phi(long long x) {
  long long ret = x;
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      ret = ret / i * (i - 1);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) ret = ret / x * (x - 1);
  return ret;
}
long long qpow(long long a, long long b, long long MOD) {
  long long ret = 1;
  while (b) {
    if (b & 1) {
      ret = ret * a;
      if (ret >= MOD) ret = ret % MOD + MOD;
    }
    b >>= 1;
    a = a * a;
    if (a >= MOD) a = a % MOD + MOD;
  }
  return ret;
}
long long dfs(long long l, long long r, long long MOD) {
  if (l == r + 1 || MOD == 1) return 1;
  return qpow(arr[l],
              dfs(l + 1, r, phi[MOD] ? phi[MOD] : phi[MOD] = count_phi(MOD)),
              MOD);
}
int main() {
  long long n, p, m;
  scanf("%lld %lld", &n, &p);
  phi[p] = count_phi(p);
  long long l, r;
  for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  scanf("%lld", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%lld %lld", &l, &r);
    printf("%lld\n", dfs(l, r, p) % p);
  }
  return 0;
}
