#include <bits/stdc++.h>
using namespace std;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int read() {
  int x = 0, w = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  return x * w;
}
const int N = 1e5 + 10;
int n, q, Mod, a[N], cnt = 0;
map<int, int> mp, pri;
void exgcd(int a, int b, int &x, int &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= 1ll * a / b * x;
}
int get_inv(int x) {
  int a, b;
  exgcd(x, Mod, a, b);
  return (a % Mod + Mod) % Mod;
}
int power(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = 1ll * a * a % Mod)
    if (b & 1) res = 1ll * res * a % Mod;
  return res;
}
struct Node {
  int sum, lazy, fac[21], val;
};
struct SegmentTree {
  Node tr[N << 2];
  void Pushup(int pos) {
    tr[pos].sum = (tr[pos << 1].sum + tr[pos << 1 | 1].sum) % Mod;
  }
  void Operater(int pos, int laz, int val) {
    tr[pos].sum = 1ll * tr[pos].sum * laz % Mod;
    tr[pos].lazy = 1ll * tr[pos].lazy * laz % Mod;
    tr[pos].val = 1ll * tr[pos].val * val % Mod;
  }
  void Pushdown(int pos) {
    Operater(pos << 1, tr[pos].lazy, tr[pos].val);
    Operater(pos << 1 | 1, tr[pos].lazy, tr[pos].val);
    tr[pos].val = 1;
    tr[pos].lazy = 1;
    for (int i = 1; i <= cnt; i++) {
      tr[pos << 1].fac[i] += tr[pos].fac[i];
      tr[pos << 1 | 1].fac[i] += tr[pos].fac[i];
      tr[pos].fac[i] = 0;
    }
  }
  void Modify_Div(int pos, int l, int r, int p, int val) {
    if (l == r) {
      for (int i = 1; i <= cnt; i++)
        while (!(val % pri[i])) tr[pos].fac[i]--, val /= pri[i];
      tr[pos].val = 1ll * tr[pos].val * get_inv(val) % Mod;
      tr[pos].sum = tr[pos].val;
      for (int i = 1; i <= cnt; i++)
        tr[pos].sum = 1ll * tr[pos].sum * power(pri[i], tr[pos].fac[i]) % Mod;
      return;
    }
    Pushdown(pos);
    int mid = (l + r) >> 1;
    if (p <= mid) Modify_Div(pos << 1, l, mid, p, val);
    if (p > mid) Modify_Div(pos << 1 | 1, mid + 1, r, p, val);
    Pushup(pos);
  }
  void Modify_Mul(int pos, int L, int R, int l, int r, int val) {
    if (l <= L && r >= R) {
      tr[pos].sum = 1ll * tr[pos].sum * val % Mod;
      tr[pos].lazy = 1ll * tr[pos].lazy * val % Mod;
      for (int i = 1; i <= cnt; i++)
        while (!(val % pri[i])) tr[pos].fac[i]++, val /= pri[i];
      tr[pos].val = 1ll * tr[pos].val * val % Mod;
      return;
    }
    int mid = (L + R) >> 1;
    Pushdown(pos);
    if (l <= mid) Modify_Mul(pos << 1, L, mid, l, r, val);
    if (r > mid) Modify_Mul(pos << 1 | 1, mid + 1, R, l, r, val);
    Pushup(pos);
  }
  int Query(int pos, int L, int R, int l, int r) {
    if (L > r || R < l) return 0;
    if (l <= L && R <= r) return tr[pos].sum;
    Pushdown(pos);
    int mid = (L + R) >> 1;
    int res1 = Query(pos << 1, L, mid, l, r),
        res2 = Query(pos << 1 | 1, mid + 1, R, l, r);
    return (res1 + res2) % Mod;
  }
  void Build(int pos, int l, int r) {
    tr[pos].lazy = 1;
    tr[pos].val = 1;
    if (l == r) {
      tr[pos].sum = a[l] % Mod;
      int x = a[l];
      for (int i = 1; i <= cnt; i++)
        while (!(x % pri[i])) tr[pos].fac[i]++, x /= pri[i];
      tr[pos].val = x % Mod;
      return;
    }
    int mid = (l + r) >> 1;
    Build(pos << 1, l, mid);
    Build(pos << 1 | 1, mid + 1, r);
    Pushup(pos);
  }
} Tr;
void init() {
  int x = Mod;
  for (int i = 2; i * i <= x; i++) {
    if (x % i) continue;
    mp[i] = ++cnt;
    pri[cnt] = i;
    while (!(x % i)) x /= i;
  }
  if (x != 1) mp[x] = ++cnt, pri[cnt] = x;
}
int main() {
  n = read();
  Mod = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  q = read();
  init();
  Tr.Build(1, 1, n);
  while (q--) {
    int opt = read();
    if (opt == 1) {
      int l = read(), r = read(), x = read();
      Tr.Modify_Mul(1, 1, n, l, r, x);
    } else if (opt == 2) {
      int pos = read(), x = read();
      Tr.Modify_Div(1, 1, n, pos, x);
    } else {
      int l = read(), r = read();
      printf("%d\n", Tr.Query(1, 1, n, l, r));
    }
  }
  return 0;
}
