#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 1 << 17, X = 1e5 + 10, P = 9, E = 17e5;
long long M;
int memo[X];
long long egcd(long long a, long long b, long long& x, long long& y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = egcd(b, a % b, y, x);
  y -= x * (a / b);
  return d;
}
inline long long inv(long long x) { return memo[x]; }
int pdiv[X], pid[X], ncid[X];
vector<int> ps, psinv, nc;
int memo2[P][E];
inline long long pw(int b, int e) {
  assert(ncid[b] != -1);
  assert(ncid[b] < P && e < E);
  return memo2[ncid[b]][e];
}
void pre() {
  iota(pdiv, pdiv + X, 0);
  memset(ncid, -1, sizeof ncid);
  memset(pid, -1, sizeof pid);
  memset(memo, -1, sizeof memo);
  for (int i = 2; i * i < X; i++) {
    if (pdiv[i] == i) {
      for (int j = i * i; j < X; j += i) {
        pdiv[j] = i;
      }
    }
  }
  for (int i = 2; i < X; i++) {
    if (pdiv[i] == i) {
      pid[i] = ps.size();
      ps.push_back(i);
      psinv.push_back(inv(i));
    }
  }
  int k = M;
  for (int i : ps) {
    if (i * i > k) break;
    if (!(k % i)) {
      ncid[i] = nc.size();
      nc.push_back(i);
      while (!(k % i)) k /= i;
    }
  }
  if (1 < k && k < X) {
    ncid[k] = nc.size();
    nc.push_back(k);
  }
  for (int i = 1; i < X; i++) {
    long long a, b;
    egcd(i, M, a, b);
    memo[i] = (a + M) % M;
  }
  for (int i = 0; i < nc.size(); i++) {
    memo2[i][0] = 1;
    for (int j = 1; j < E; j++) {
      memo2[i][j] = (long long)memo2[i][j - 1] * nc[i] % M;
    }
  }
}
struct val {
  long long cp;
  vector<int> ncp;
  val() : cp(1), ncp(nc.size()) {}
  val(int x) : val() {
    while (x > 1) {
      int p = pdiv[x];
      if (ncid[p] != -1) {
        ncp[ncid[p]]++;
      } else {
        cp = cp * p % M;
      }
      x /= p;
    }
  }
  val operator*=(const val& o) {
    cp = cp * o.cp % M;
    for (int i = 0; i < ncp.size(); i++) {
      ncp[i] += o.ncp[i];
    }
    return *this;
  }
  val operator/=(const val& o) {
    cp = cp * inv(o.cp) % M;
    for (int i = 0; i < ncp.size(); i++) {
      ncp[i] -= o.ncp[i];
    }
    return *this;
  }
  val operator+(const val& o) const {
    val ret;
    ret.cp = (getv() + o.getv()) % M;
    return ret;
  }
  long long getv() const {
    long long ans = cp;
    for (int i = 0; i < ncp.size(); i++) {
      if (ncp[i]) {
        ans = ans * pw(nc[i], ncp[i]) % M;
      }
    }
    return ans;
  }
};
namespace s {
val t[2 * N], lazy[2 * N];
bool needpush[2 * N];
void push(int i) {
  if (needpush[i]) {
    t[2 * i] *= lazy[i];
    t[2 * i + 1] *= lazy[i];
    lazy[2 * i] *= lazy[i];
    lazy[2 * i + 1] *= lazy[i];
    needpush[2 * i] = needpush[2 * i + 1] = 1;
    needpush[i] = 0;
    lazy[i] = val();
  }
}
void pull(int i) { t[i] = t[2 * i] + t[2 * i + 1]; }
void build() {
  for (int i = N - 1; i >= 1; i--) {
    pull(i);
  }
}
void mul(int x, int y, const val& v, int i = 1, int l = 0, int r = N - 1) {
  if (y < l || r < x) return;
  if (x <= l && r <= y) {
    t[i] *= v;
    lazy[i] *= v;
    needpush[i] = 1;
    return;
  }
  int m = (l + r) / 2;
  push(i);
  mul(x, y, v, 2 * i, l, m);
  mul(x, y, v, 2 * i + 1, m + 1, r);
  pull(i);
}
void div(int x, const val& v, int i = 1, int l = 0, int r = N - 1) {
  if (i >= N) {
    t[i] /= v;
    return;
  }
  int m = (l + r) / 2;
  push(i);
  if (x <= m) {
    div(x, v, 2 * i, l, m);
  } else {
    div(x, v, 2 * i + 1, m + 1, r);
  }
  pull(i);
}
long long query(int x, int y, int i = 1, int l = 0, int r = N - 1) {
  if (y < l || r < x) return 0;
  if (x <= l && r <= y) return t[i].getv();
  int m = (l + r) / 2;
  push(i);
  return (query(x, y, 2 * i, l, m) + query(x, y, 2 * i + 1, m + 1, r));
}
}  // namespace s
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n >> M;
  pre();
  for (int i = 0; i < 2 * N; i++) {
    s::t[i] = val();
    s::lazy[i] = val();
  }
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s::t[N + i] = val(a);
  }
  s::build();
  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int l, r, x;
      cin >> l >> r >> x;
      l--;
      r--;
      s::mul(l, r, val(x));
    } else if (t == 2) {
      int p, x;
      cin >> p >> x;
      p--;
      s::div(p, val(x));
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      cout << s::query(l, r) % M << nl;
    }
  }
  return 0;
}
