#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
struct SegTree {
  int n, M;
  vector<int> lz, tr;
  SegTree() {}
  SegTree(int n, int M, int *a) : n(n), M(M), lz(4 * n), tr(4 * n) {
    build(1, 1, n, a);
  }
  int combine(int left, int right) {
    int ans = left + right;
    if (ans >= M) ans -= M;
    return ans;
  }
  void propagate(int u, int st, int en) {
    if (lz[u] == 1) return;
    tr[u] = (1LL * tr[u] * lz[u]) % M;
    if (st != en) {
      lz[2 * u] = (1LL * lz[u] * lz[2 * u]) % M;
      lz[2 * u + 1] = (1LL * lz[u] * lz[2 * u + 1]) % M;
    }
    lz[u] = 1;
  }
  void build(int u, int st, int en, int *a) {
    if (st == en) {
      tr[u] = a[st] % M;
      lz[u] = 1;
    } else {
      int mid = (st + en) / 2;
      build(2 * u, st, mid, a);
      build(2 * u + 1, mid + 1, en, a);
      tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
      lz[u] = 1;
    }
  }
  void update(int u, int st, int en, int l, int r, int x) {
    propagate(u, st, en);
    if (r < st || en < l)
      return;
    else if (l <= st && en <= r) {
      lz[u] = (1LL * lz[u] * x) % M;
      propagate(u, st, en);
    } else {
      int mid = (st + en) / 2;
      update(2 * u, st, mid, l, r, x);
      update(2 * u + 1, mid + 1, en, l, r, x);
      tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
    }
  }
  void Set(int u, int st, int en, int idx, int val) {
    propagate(u, st, en);
    if (idx < st || en < idx)
      return;
    else if (st == en)
      tr[u] = val;
    else {
      int mid = (st + en) / 2;
      Set(2 * u, st, mid, idx, val);
      Set(2 * u + 1, mid + 1, en, idx, val);
      tr[u] = combine(tr[2 * u], tr[2 * u + 1]);
    }
  }
  int query(int u, int st, int en, int l, int r) {
    propagate(u, st, en);
    if (r < st || en < l) return 0;
    if (l <= st && en <= r)
      return tr[u];
    else {
      int mid = (st + en) / 2;
      return combine(query(2 * u, st, mid, l, r),
                     query(2 * u + 1, mid + 1, en, l, r));
    }
  }
};
struct Fenwick {
  int n;
  vector<int> ft;
  Fenwick() {}
  Fenwick(int n) : n(n), ft(n + 1) {}
  void update(int idx, int val) {
    for (; idx > 0; idx -= idx & -idx) ft[idx] += val;
  }
  int get(int idx) {
    int sum = 0;
    for (; idx <= n; idx += idx & -idx) sum += ft[idx];
    return sum;
  }
  void update(int l, int r, int val) {
    update(r, val);
    update(l - 1, -val);
    update(r, 0);
    update(l - 1, 0);
  }
};
Fenwick ft[10];
vector<int> factor(int x) {
  vector<int> pf;
  for (int i = 2; i * i <= x; i++) {
    if (x % i) continue;
    while (x % i == 0) x /= i;
    pf.push_back(i);
  }
  if (x > 1) pf.push_back(x);
  return pf;
}
int a[N], b[N];
const int MX = 4e6 + 7;
int power[10][MX];
int n, mod;
vector<int> pf;
long long egcd(long long a, long long b, long long &x, long long &y) {
  long long xx = y = 0;
  long long yy = x = 1;
  while (b) {
    long long q = a / b;
    long long t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
long long inverse(long long a, long long m) {
  long long x, y;
  long long g = egcd(a, m, x, y);
  if (g > 1) return -1;
  return (x % m + m) % m;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> mod;
  pf = factor(mod);
  int k = pf.size();
  for (int i = 0; i < k; i++) {
    int p = pf[i];
    ft[i] = Fenwick(n);
    power[i][0] = 1;
    for (int j = 1; j < MX; j++)
      power[i][j] = (1LL * power[i][j - 1] * p) % mod;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int x = a[i];
    for (int j = 0; j < k; j++) {
      int p = pf[j];
      int cnt = 0;
      while (x % p == 0) x /= p, cnt++;
      if (cnt == 0) continue;
      ft[j].update(i, i, cnt);
    }
    b[i] = x;
  }
  SegTree st(n, mod, a), stp(n, mod, b);
  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int l, r, x;
      cin >> l >> r >> x;
      st.update(1, 1, n, l, r, x);
      for (int i = 0; i < k; i++) {
        int p = pf[i];
        int cnt = 0;
        while (x % p == 0) x /= p, cnt++;
        if (cnt == 0) continue;
        ft[i].update(l, r, cnt);
      }
      stp.update(1, 1, n, l, r, x);
    } else if (t == 2) {
      int k, x;
      cin >> k >> x;
      int cur = 1;
      for (int i = 0; i < pf.size(); i++) {
        int p = pf[i];
        int cnt = 0;
        while (x % p == 0) x /= p, cnt++;
        if (cnt) ft[i].update(k, k, -cnt);
        int trucnt = ft[i].get(k);
        cur = (1LL * cur * power[i][trucnt]) % mod;
      }
      stp.update(1, 1, n, k, k, inverse(x, mod));
      cur = (1LL * cur * stp.query(1, 1, n, k, k)) % mod;
      st.Set(1, 1, n, k, cur);
    } else if (t == 3) {
      int l, r;
      cin >> l >> r;
      cout << st.query(1, 1, n, l, r) << "\n";
    }
  }
}
