#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  int v = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
inline void read(long long &x) {
  long long v = 0ll, f = 1ll;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-')
    f = -1;
  else
    v = v * 10 + c - '0';
  while (isdigit(c = getchar())) v = v * 10 + c - '0';
  x = v * f;
}
inline void readc(char &x) {
  char c;
  while ((c = getchar()) == ' ')
    ;
  x = c;
}
inline void writes(string s) { puts(s.c_str()); }
inline void writeln() { writes(""); }
inline void writei(int x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
inline void writell(long long x) {
  if (x < 0) {
    putchar('-');
    x = abs(x);
  }
  if (!x) putchar('0');
  char a[25];
  int top = 0;
  while (x) {
    a[++top] = (x % 10) + '0';
    x /= 10;
  }
  while (top) {
    putchar(a[top]);
    top--;
  }
}
int n, m, i, j, mod, t, lt[400005], rt[400005], ss[400005], sum[400005],
    mul[400005], add[400005][11], pw[11][10005], pw2[11][10005], a[100005];
vector<int> p;
int exg(int x, int y, int &a, int &b) {
  if (!y) {
    a = 1;
    b = 0;
    return x;
  }
  int ans = exg(y, x % y, b, a);
  b -= x / y * a;
  return ans;
}
int inv(int x, int y) {
  int a, b;
  int t = exg(x, y, a, b);
  if (t != 1) exit(-1);
  if (a < 0) a += y;
  return a;
}
int qp(int x, int y) {
  if (!y) return 1;
  int z = qp(x, y / 2);
  z = 1ll * z * z % mod;
  if (y & 1) z = 1ll * z * x % mod;
  return z;
}
inline int gpw(int id, int y) {
  return 1ll * pw[id][y % 10000] * pw2[id][y / 10000] % mod;
}
inline void upd(int x, int y) {
  int i;
  ss[x] = 1ll * ss[x] * y % mod;
  if ((0) <= (p.size() - 1))
    for ((i) = (0); (i) <= (p.size() - 1); (i)++) {
      while (y % p[i] == 0) {
        add[x][i]++;
        y /= p[i];
      }
    }
  sum[x] = 1ll * sum[x] * y % mod;
  mul[x] = 1ll * mul[x] * y % mod;
}
inline void pushup(int x) {
  sum[x] = (sum[x * 2] + sum[x * 2 + 1]) % mod;
  ss[x] = (ss[x * 2] + ss[x * 2 + 1]) % mod;
  int i;
}
inline void pushdo(int x) {
  if (mul[x] != 1) {
    ss[x * 2] = 1ll * ss[x * 2] * mul[x] % mod;
    ss[x * 2 + 1] = 1ll * ss[x * 2 + 1] * mul[x] % mod;
    sum[x * 2] = 1ll * sum[x * 2] * mul[x] % mod;
    sum[x * 2 + 1] = 1ll * sum[x * 2 + 1] * mul[x] % mod;
    mul[x * 2] = 1ll * mul[x * 2] * mul[x] % mod;
    mul[x * 2 + 1] = 1ll * mul[x * 2 + 1] * mul[x] % mod;
    mul[x] = 1;
  }
  int i;
  if ((0) <= (p.size() - 1))
    for ((i) = (0); (i) <= (p.size() - 1); (i)++)
      if (add[x][i]) {
        if (add[x][i] < 0) exit(15);
        ss[x * 2] = 1ll * ss[x * 2] * gpw(i, add[x][i]) % mod;
        ss[x * 2 + 1] = 1ll * ss[x * 2 + 1] * gpw(i, add[x][i]) % mod;
        add[x * 2][i] += add[x][i];
        add[x * 2 + 1][i] += add[x][i];
        add[x][i] = 0;
      }
}
void build(int x, int l, int r) {
  lt[x] = l;
  rt[x] = r;
  mul[x] = 1;
  if (l == r) {
    ss[x] = sum[x] = 1;
    upd(x, a[l]);
    return;
  }
  build(x * 2, l, (l + r) / 2);
  build(x * 2 + 1, (l + r) / 2 + 1, r);
  pushup(x);
}
void update(int x, int l, int r, int c) {
  if (lt[x] > r || rt[x] < l) return;
  if (lt[x] >= l && rt[x] <= r) {
    upd(x, c);
    return;
  }
  pushdo(x);
  update(x * 2, l, r, c);
  update(x * 2 + 1, l, r, c);
  pushup(x);
}
void update2(int x, int y, int c) {
  if (lt[x] > y || rt[x] < y) return;
  if (lt[x] == rt[x]) {
    int i;
    if ((0) <= (p.size() - 1))
      for ((i) = (0); (i) <= (p.size() - 1); (i)++) {
        while (c % p[i] == 0) {
          add[x][i]--;
          c /= p[i];
        }
      }
    sum[x] = 1ll * inv(c, mod) * sum[x] % mod;
    ss[x] = sum[x];
    if ((0) <= (p.size() - 1))
      for ((i) = (0); (i) <= (p.size() - 1); (i)++)
        if (add[x][i]) {
          if (add[x][i] < 0) exit(-15);
          ss[x] = 1ll * ss[x] * gpw(i, add[x][i]) % mod;
        }
    return;
  }
  pushdo(x);
  update2(x * 2, y, c);
  update2(x * 2 + 1, y, c);
  pushup(x);
}
int query(int x, int l, int r) {
  if (lt[x] > r || rt[x] < l) return 0;
  if (lt[x] >= l && rt[x] <= r) return ss[x];
  pushdo(x);
  return (query(x * 2, l, r) + query(x * 2 + 1, l, r)) % mod;
}
int main() {
  read(n);
  read(mod);
  if ((1) <= ((n)))
    for (((i)) = (1); ((i)) <= ((n)); ((i))++) read(a[i]);
  t = mod;
  for (i = 2; i * i <= t; i++)
    if (t % i == 0) {
      p.push_back(i);
      while (t % i == 0) t /= i;
    }
  if (t > 1) p.push_back(t);
  if ((0) <= (p.size() - 1))
    for ((i) = (0); (i) <= (p.size() - 1); (i)++) {
      pw[i][0] = 1;
      if ((1) <= ((10000)))
        for (((j)) = (1); ((j)) <= ((10000)); ((j))++)
          pw[i][j] = 1ll * pw[i][j - 1] * p[i] % mod;
      pw2[i][0] = 1;
      if ((1) <= ((10000)))
        for (((j)) = (1); ((j)) <= ((10000)); ((j))++)
          pw2[i][j] = 1ll * pw[i][10000] * pw2[i][j - 1] % mod;
    }
  build(1, 1, n);
  read(m);
  while (m--) {
    int op, x, y, z;
    read(op);
    if (op == 1) {
      read(x);
      read(y);
      read(z);
      update(1, x, y, z);
    }
    if (op == 2) {
      read(x);
      read(y);
      update2(1, x, y);
    }
    if (op == 3) {
      read(x);
      read(y);
      printf("%d\n", query(1, x, y));
    }
  }
  return 0;
}
