#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &a) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  a = x * f;
}
template <typename T, typename... Args>
void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
long long n, q, mod, a[100005], cnt = 0;
map<long long, long long> mp, hxb;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
void exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= 1ll * a / b * x;
}
long long inv(long long x) {
  long long a, b;
  exgcd(x, mod, a, b);
  return (a % mod + mod) % mod;
}
long long ksm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = 1ll * res * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return res;
}
struct Segment_Tree {
  long long sum[100005 << 2], lazy[100005 << 2], fac[100005 << 2][15],
      tr[100005 << 2];
  void pushup(long long now) {
    sum[now] = (sum[now << 1] + sum[now << 1 | 1]) % mod;
  }
  void pushdown(long long now) {
    sum[now << 1] = 1ll * sum[now << 1] * lazy[now] % mod;
    lazy[now << 1] = 1ll * lazy[now << 1] * lazy[now] % mod;
    tr[now << 1] = 1ll * tr[now << 1] * tr[now] % mod;
    sum[now << 1 | 1] = 1ll * sum[now << 1 | 1] * lazy[now] % mod;
    lazy[now << 1 | 1] = 1ll * lazy[now << 1 | 1] * lazy[now] % mod;
    tr[now << 1 | 1] = 1ll * tr[now << 1 | 1] * tr[now] % mod;
    tr[now] = 1;
    lazy[now] = 1;
    for (long long i = 1; i <= cnt; i++) {
      fac[now << 1][i] += fac[now][i];
      fac[now << 1 | 1][i] += fac[now][i];
      fac[now][i] = 0;
    }
  }
  void build(long long now, long long l, long long r) {
    lazy[now] = 1;
    tr[now] = 1;
    if (l == r) {
      sum[now] = a[l] % mod;
      long long x = a[l];
      for (long long i = 1; i <= cnt; i++)
        while (!(x % hxb[i])) fac[now][i]++, x /= hxb[i];
      tr[now] = x % mod;
      return;
    }
    long long mid = (l + r) >> 1;
    build(now << 1, l, mid);
    build(now << 1 | 1, mid + 1, r);
    pushup(now);
  }
  void update(long long now, long long l, long long r, long long p,
              long long x) {
    if (l == r) {
      for (long long i = 1; i <= cnt; i++)
        while (!(x % hxb[i])) fac[now][i]--, x /= hxb[i];
      tr[now] = 1ll * tr[now] * inv(x) % mod;
      sum[now] = tr[now];
      for (long long i = 1; i <= cnt; i++)
        sum[now] = 1ll * sum[now] * ksm(hxb[i], fac[now][i]) % mod;
      return;
    }
    pushdown(now);
    long long mid = (l + r) >> 1;
    if (p <= mid) update(now << 1, l, mid, p, x);
    if (p > mid) update(now << 1 | 1, mid + 1, r, p, x);
    pushup(now);
  }
  void update(long long now, long long l, long long r, long long ll,
              long long rr, long long x) {
    if (ll <= l && rr >= r) {
      sum[now] = 1ll * sum[now] * x % mod;
      lazy[now] = 1ll * lazy[now] * x % mod;
      for (long long i = 1; i <= cnt; i++)
        while (!(x % hxb[i])) fac[now][i]++, x /= hxb[i];
      tr[now] = tr[now] * x % mod;
      return;
    }
    long long mid = (l + r) >> 1;
    pushdown(now);
    if (ll <= mid) update(now << 1, l, mid, ll, rr, x);
    if (rr > mid) update(now << 1 | 1, mid + 1, r, ll, rr, x);
    pushup(now);
  }
  long long query(long long now, long long l, long long r, long long ll,
                  long long rr) {
    if (ll <= l && rr >= r) {
      return sum[now];
    }
    pushdown(now);
    long long mid = (l + r) >> 1, ans = 0;
    if (ll <= mid) ans = (ans + query(now << 1, l, mid, ll, rr)) % mod;
    if (rr > mid) ans = (ans + query(now << 1 | 1, mid + 1, r, ll, rr)) % mod;
    return ans;
  }
} T;
signed main() {
  read(n, mod);
  long long _mod = mod;
  for (long long i = 2; i * i <= _mod; i++) {
    if (_mod % i) continue;
    mp[i] = ++cnt;
    hxb[cnt] = i;
    while (!(_mod % i)) _mod /= i;
  }
  if (_mod != 1) mp[_mod] = ++cnt, hxb[cnt] = _mod;
  for (long long i = 1; i <= n; i++) read(a[i]);
  T.build(1, 1, n);
  read(q);
  while (q--) {
    long long opt;
    read(opt);
    if (opt == 1) {
      long long l, r, x;
      read(l, r, x);
      T.update(1, 1, n, l, r, x);
    }
    if (opt == 2) {
      long long p, x;
      read(p, x);
      T.update(1, 1, n, p, x);
    }
    if (opt == 3) {
      long long l, r;
      read(l, r);
      long long ans = T.query(1, 1, n, l, r);
      printf("%d\n", ans);
    }
  }
  return 0;
}
