#include <bits/stdc++.h>
using namespace std;
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, P, a[100010], q, L[100010 << 2], R[100010 << 2], ans[100010 << 2], p[12],
    u[12][2][1000010], cnt;
long long lazy[100010 << 2][12];
int ksm(int a, long long k) {
  return 1ll * u[a][0][k % 1000000] * u[a][1][k / 1000000] % P;
}
void exgcd(int &x, int &y, int a, int b) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  exgcd(x, y, b, a % b);
  int t = x;
  x = y;
  y = t - a / b * x;
}
int inv(int a) {
  int x, y;
  exgcd(x, y, a, P);
  return (x % P + P) % P;
}
void up(int k) { ans[k] = (ans[k << 1] + ans[k << 1 | 1]) % P; }
void update(int k) {
  ans[k] = lazy[k][0];
  for (int i = 1; i <= cnt; i++) ans[k] = 1ll * ans[k] * ksm(i, lazy[k][i]) % P;
}
void down(int k, int i) {
  lazy[k << 1][i] += lazy[k][i], lazy[k << 1 | 1][i] += lazy[k][i];
  ans[k << 1] = 1ll * ans[k << 1] * ksm(i, lazy[k][i]) % P,
           ans[k << 1 | 1] = 1ll * ans[k << 1 | 1] * ksm(i, lazy[k][i]) % P;
  lazy[k][i] = 0;
}
void down(int k) {
  lazy[k << 1][0] = 1ll * lazy[k << 1][0] * lazy[k][0] % P;
  lazy[k << 1 | 1][0] = 1ll * lazy[k << 1 | 1][0] * lazy[k][0] % P;
  ans[k << 1] = 1ll * ans[k << 1] * lazy[k][0] % P;
  ans[k << 1 | 1] = 1ll * ans[k << 1 | 1] * lazy[k][0] % P;
  lazy[k][0] = 1;
  for (int i = 1; i <= cnt; i++) down(k, i);
}
void build(int k, int l, int r) {
  L[k] = l, R[k] = r;
  lazy[k][0] = 1;
  if (l == r) {
    ans[k] = lazy[k][0] = a[l];
    ans[k] %= P;
    for (int i = 1; i <= cnt; i++)
      while (lazy[k][0] % p[i] == 0) lazy[k][0] /= p[i], lazy[k][i]++;
    return;
  }
  int mid = l + r >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
  up(k);
}
void add(int k, int l, int r, int i, int x, int u) {
  if (L[k] == l && R[k] == r) {
    ans[k] = 1ll * ans[k] * u % P;
    lazy[k][i] += x;
    return;
  }
  down(k, i);
  int mid = L[k] + R[k] >> 1;
  if (r <= mid)
    add(k << 1, l, r, i, x, u);
  else if (l > mid)
    add(k << 1 | 1, l, r, i, x, u);
  else
    add(k << 1, l, mid, i, x, u), add(k << 1 | 1, mid + 1, r, i, x, u);
  up(k);
}
void mul(int k, int l, int r, int x) {
  if (L[k] == l && R[k] == r) {
    ans[k] = 1ll * ans[k] * x % P;
    lazy[k][0] = 1ll * lazy[k][0] * x % P;
    return;
  }
  down(k);
  int mid = L[k] + R[k] >> 1;
  if (r <= mid)
    mul(k << 1, l, r, x);
  else if (l > mid)
    mul(k << 1 | 1, l, r, x);
  else
    mul(k << 1, l, mid, x), mul(k << 1 | 1, mid + 1, r, x);
  up(k);
}
void add(int k, int p, int i, int x) {
  if (L[k] == R[k]) {
    lazy[k][i] += x;
    update(k);
    return;
  }
  down(k, i);
  int mid = L[k] + R[k] >> 1;
  if (p <= mid)
    add(k << 1, p, i, x);
  else
    add(k << 1 | 1, p, i, x);
  up(k);
}
void mul(int k, int p, int x) {
  if (L[k] == R[k]) {
    lazy[k][0] = 1ll * lazy[k][0] * x % P;
    update(k);
    return;
  }
  down(k);
  int mid = L[k] + R[k] >> 1;
  if (p <= mid)
    mul(k << 1, p, x);
  else
    mul(k << 1 | 1, p, x);
  up(k);
}
int query(int k, int l, int r) {
  if (L[k] == l && R[k] == r) return ans[k];
  down(k);
  int mid = L[k] + R[k] >> 1;
  if (r <= mid)
    return query(k << 1, l, r);
  else if (l > mid)
    return query(k << 1 | 1, l, r);
  else
    return (query(k << 1, l, mid) + query(k << 1 | 1, mid + 1, r)) % P;
}
signed main() {
  n = read(), P = read();
  int v = P;
  for (int i = 2; i * i <= v; i++)
    if (v % i == 0) {
      p[++cnt] = i;
      while (v % i == 0) v /= i;
    }
  if (v > 1) p[++cnt] = v;
  for (int i = 1; i <= cnt; i++) {
    u[i][0][0] = 1;
    for (int j = 1; j <= 1000000; j++)
      u[i][0][j] = 1ll * u[i][0][j - 1] * p[i] % P;
    u[i][1][0] = 1;
    for (int j = 1; j <= 1000000; j++)
      u[i][1][j] = 1ll * u[i][1][j - 1] * u[i][0][1000000] % P;
  }
  for (int i = 1; i <= n; i++) a[i] = read();
  build(1, 1, n);
  q = read();
  while (q--) {
    int op = read();
    if (op == 1) {
      int l = read(), r = read(), x = read();
      for (int i = 1; i <= cnt; i++) {
        int t = 0;
        while (x % p[i] == 0) t++, x /= p[i];
        if (t) add(1, l, r, i, t, ksm(i, t));
      }
      mul(1, l, r, x);
    }
    if (op == 2) {
      int u = read(), x = read();
      for (int i = 1; i <= cnt; i++) {
        int t = 0;
        while (x % p[i] == 0) t++, x /= p[i];
        if (t) add(1, u, i, -t);
      }
      mul(1, u, inv(x));
    }
    if (op == 3) {
      int l = read(), r = read();
      printf("%d\n", query(1, l, r));
    }
  }
  return 0;
}
