#include <bits/stdc++.h>
clock_t t = clock();
namespace my_std {
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
inline T rnd(T l, T r) {
  return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &t) {
  t = 0;
  char f = 0, ch = getchar();
  double d = 0.1;
  while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
  while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar();
  if (ch == '.') {
    ch = getchar();
    while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar();
  }
  t = (f ? -t : t);
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
char __sr[1 << 21], __z[20];
int __C = -1, __zz = 0;
inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; }
inline void print(register int x) {
  if (__C > 1 << 20) Ot();
  if (x < 0) __sr[++__C] = '-', x = -x;
  while (__z[++__zz] = x % 10 + 48, x /= 10)
    ;
  while (__sr[++__C] = __z[__zz], --__zz)
    ;
  __sr[++__C] = '\n';
}
void file() {}
inline void chktime() {}
}  // namespace my_std
using namespace my_std;
int n;
long long mod;
int p[25], cnt;
int a[101010];
long long ksm(long long x, int y) {
  long long ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1) ret = ret * x % mod;
  return ret;
}
void exgcd(int a, int b, long long &x, long long &y) {
  if (!b) return (void)(x = 1, y = 0);
  exgcd(b, a % b, y, x);
  (y -= (a / b * x) % mod - mod) %= mod;
}
long long inv(long long a) {
  long long x, y;
  exgcd(a, mod, x, y);
  x = (x % mod + mod) % mod;
  return x;
}
long long c[101010 << 2][25];
long long sum[101010 << 2];
void pushup(int k) { sum[k] = (sum[k << 1] + sum[k << 1 | 1]) % mod; }
void pushdown(int k) {
  long long S = 1;
  long long *t = c[k], *Ls = c[k << 1], *Rs = c[k << 1 | 1];
  for (int i = (1); i <= (cnt); i++)
    S = S * ksm(p[i], t[i]) % mod, Ls[i] += t[i], Rs[i] += t[i], t[i] = 0;
  long long x = t[0];
  (Ls[0] *= x) %= mod;
  (Rs[0] *= x) %= mod;
  (S *= x) %= mod;
  t[0] = 1;
  sum[k << 1] = sum[k << 1] * S % mod;
  sum[k << 1 | 1] = sum[k << 1 | 1] * S % mod;
}
void build(int k, int l, int r) {
  c[k][0] = 1;
  if (l == r) {
    sum[k] = a[l] % mod;
    for (int i = (1); i <= (cnt); i++)
      while (a[l] % p[i] == 0) ++c[k][i], a[l] /= p[i];
    c[k][0] = a[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
  pushup(k);
}
long long query(int k, int l, int r, int x, int y) {
  if (x <= l && r <= y) return sum[k];
  int mid = (l + r) >> 1;
  long long ret = 0;
  pushdown(k);
  if (x <= mid) ret += query(k << 1, l, mid, x, y);
  if (y > mid) ret += query(k << 1 | 1, mid + 1, r, x, y);
  return ret % mod;
}
void modify(int k, int l, int r, int x, int *s) {
  if (l == r) {
    long long S = 1;
    for (int i = (1); i <= (cnt); i++)
      c[k][i] -= s[i], S = S * ksm(p[i], c[k][i]) % mod;
    c[k][0] = c[k][0] * inv(s[0]) % mod;
    S = S * c[k][0] % mod;
    sum[k] = S;
    return;
  }
  pushdown(k);
  int mid = (l + r) >> 1;
  if (x <= mid)
    modify(k << 1, l, mid, x, s);
  else
    modify(k << 1 | 1, mid + 1, r, x, s);
  pushup(k);
}
void modify(int k, int l, int r, int x, int y, int *s) {
  if (x <= l && r <= y) {
    long long S = 1;
    for (int i = (1); i <= (cnt); i++)
      c[k][i] += s[i], S = S * ksm(p[i], s[i]) % mod;
    (c[k][0] *= s[0]) %= mod;
    (S *= s[0]) %= mod;
    (sum[k] *= S) %= mod;
    return;
  }
  pushdown(k);
  int mid = (l + r) >> 1;
  if (x <= mid) modify(k << 1, l, mid, x, y, s);
  if (y > mid) modify(k << 1 | 1, mid + 1, r, x, y, s);
  pushup(k);
}
void dfs(int k, int l, int r) {
  if (l == r) {
    long long ret = 1;
    for (int i = (1); i <= (cnt); i++) ret = ret * ksm(p[i], c[k][i]) % mod;
    ret *= c[k][0];
    printf("%lld ", ret);
    return;
  }
  pushdown(k);
  int mid = (l + r) >> 1;
  dfs(k << 1, l, mid);
  dfs(k << 1 | 1, mid + 1, r);
}
int s[101010];
void divide(int x) {
  for (int i = (1); i <= (cnt); i++) s[i] = 0;
  for (int i = (1); i <= (cnt); i++)
    while (x % p[i] == 0) ++s[i], x /= p[i];
  s[0] = x;
}
int main() {
  file();
  read(n, mod);
  int mm = mod;
  for (int i = (2); i <= (sqrt(mod)); i++)
    if (mm % i == 0) {
      p[++cnt] = i;
      while (mm % i == 0) mm /= i;
    }
  if (mm != 1) p[++cnt] = mm;
  for (int i = (1); i <= (n); i++) read(a[i]);
  build(1, 1, n);
  int Q;
  read(Q);
  int opt, x, y, z;
  while (Q--) {
    read(opt);
    if (opt == 1)
      read(x, y, z), divide(z), modify(1, 1, n, x, y, s);
    else if (opt == 2)
      read(x, y), divide(y), modify(1, 1, n, x, s);
    else
      read(x, y), printf("%lld\n", query(1, 1, n, x, y) % mod);
  }
  return 0;
}
