#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;
int M;
int memo[X];
void egcd(long long a, long long b, long long& x, long long& y) {
  if (!b) {
    x = 1;
    y = 0;
    return;
  }
  egcd(b, a % b, y, x);
  y -= x * (a / b);
}
inline int inv(long long x) { return memo[x]; }
int pdiv[X], pid[X], ncid[X];
vector<int> ps, nc;
vector<int> memo2[P];
inline long long pw(int b, int 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);
    }
  }
  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++) {
    int lim = X * log(X) / log(nc[i]) + 1;
    memo2[i].resize(lim + 1);
    memo2[i][0] = 1;
    for (int j = 1; j <= lim; j++) {
      memo2[i][j] = (long long)memo2[i][j - 1] * nc[i] % M;
    }
  }
}
struct val {
  long long cp, v;
  vector<int> ncp;
  val() : cp(1), v(1), ncp(nc.size()) {}
  val(int x) : val() {
    v = x;
    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];
    }
    v = v * o.v % M;
    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];
    }
    v = getv();
    return *this;
  }
  val operator+(const val& o) const {
    val ret;
    ret.cp = ret.v = (v + o.v) % 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[N];
bool needpush[N];
inline void push(int i) {
  if (needpush[i]) {
    t[2 * i] *= lazy[i];
    t[2 * i + 1] *= lazy[i];
    if (2 * i < N) {
      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();
  }
}
inline 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;
    if (i < N) {
      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].v;
  int m = (l + r) / 2;
  push(i);
  return (query(x, y, 2 * i, l, m) + query(x, y, 2 * i + 1, m + 1, r)) % M;
}
}  // namespace s
inline char get() {
  static char buf[100000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
inline void read(int& x) {
  static char c;
  x = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    ;
  for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  read(n);
  read(M);
  pre();
  for (int i = 1; i < N; i++) {
    s::t[i] = val();
    s::lazy[i] = val();
  }
  for (int i = 0; i < n; i++) {
    int a;
    read(a);
    s::t[N + i] = val(a);
  }
  s::build();
  int q;
  read(q);
  while (q--) {
    int t;
    read(t);
    if (t == 1) {
      int l, r, x;
      read(l);
      read(r);
      read(x);
      l--;
      r--;
      s::mul(l, r, val(x));
    } else if (t == 2) {
      int p, x;
      read(p);
      read(x);
      p--;
      s::div(p, val(x));
    } else {
      int l, r;
      read(l);
      read(r);
      l--;
      r--;
      cout << s::query(l, r) << nl;
    }
  }
  return 0;
}
