#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void read(Tp &x) {
  static char c;
  static bool neg;
  x = 0, c = getchar(), neg = false;
  for (; !isdigit(c); c = getchar()) {
    if (c == '-') {
      neg = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (neg) {
    x = -x;
  }
}
int exgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = exgcd(b, a % b, x, y);
    x -= (a / b) * y;
    swap(x, y);
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
inline int inv(int a, int mod) {
  static int x, y;
  exgcd(a, mod, x, y);
  return x;
}
const int N = 1e5 + 5;
int n, mod, phi;
int a[N];
int q;
inline int power(int base, int exp) {
  int res = 1;
  while (exp > 0) {
    if (exp & 1) {
      res = (long long)res * base % mod;
    }
    base = (long long)base * base % mod;
    exp >>= 1;
  }
  return res;
}
vector<int> primes;
bool notPrime[N];
inline void sieves() {
  notPrime[1] = true;
  primes.reserve(N / 10);
  for (int i = 2; i < N; ++i) {
    if (!notPrime[i]) {
      primes.emplace_back(i);
    }
    for (const auto &p : primes) {
      if (i * p >= N) {
        break;
      }
      notPrime[i * p] = true;
      if (i % p == 0) {
        break;
      }
    }
  }
}
vector<int> facOfMod;
inline void factor() {
  int x = mod;
  for (const auto &p : primes) {
    if (x % p == 0) {
      facOfMod.emplace_back(p);
      do {
        x /= p;
      } while (x % p == 0);
    }
  }
  if (x != 1) {
    facOfMod.emplace_back(x);
  }
}
inline void getPhi() {
  int x = mod;
  phi = x;
  for (int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      phi -= phi / i;
      do {
        x /= i;
      } while (x % i == 0);
    }
  }
  if (x > 1) {
    phi -= phi / x;
  }
}
inline void modInit() {
  sieves();
  factor();
  getPhi();
}
inline int divFac(int &x, int p) {
  int res = 0;
  while (x % p == 0) {
    x /= p;
    ++res;
  }
  return res;
}
namespace Seg {
int sum[N * 4], tag[N * 4], val[N * 4], cnt[N * 4][12];
inline void pushUp(int u) {
  sum[u] = (sum[((u) << 1)] + sum[((u) << 1 | 1)]) % mod;
}
inline void pushDown(int u) {
  for (int i = 0; i < (int)facOfMod.size(); ++i) {
    if (cnt[u][i] != 0) {
      cnt[((u) << 1)][i] += cnt[u][i];
      cnt[((u) << 1 | 1)][i] += cnt[u][i];
      cnt[u][i] = 0;
    }
  }
  if (tag[u] != 1) {
    sum[((u) << 1)] = (long long)sum[((u) << 1)] * tag[u] % mod;
    sum[((u) << 1 | 1)] = (long long)sum[((u) << 1 | 1)] * tag[u] % mod;
    tag[((u) << 1)] = (long long)tag[((u) << 1)] * tag[u] % mod;
    tag[((u) << 1 | 1)] = (long long)tag[((u) << 1 | 1)] * tag[u] % mod;
    tag[u] = 1;
  }
  if (val[u] != 1) {
    val[((u) << 1)] = (long long)val[((u) << 1)] * val[u] % mod;
    val[((u) << 1 | 1)] = (long long)val[((u) << 1 | 1)] * val[u] % mod;
    val[u] = 1;
  }
}
void build(int u, int l, int r) {
  tag[u] = 1;
  if (l == r) {
    sum[u] = a[l] % mod;
    int x = a[l];
    for (int i = 0; i < (int)facOfMod.size(); ++i) {
      cnt[u][i] = divFac(x, facOfMod[i]);
    }
    val[u] = x % mod;
    return;
  }
  val[u] = 1;
  int mid = (l + r) >> 1;
  build(((u) << 1), l, mid);
  build(((u) << 1 | 1), mid + 1, r);
  pushUp(u);
}
int exp[12], left;
inline void calcExp(int x) {
  for (int i = 0; i < (int)facOfMod.size(); ++i) {
    exp[i] = divFac(x, facOfMod[i]);
  }
  left = x;
}
void mul(int u, int l, int r, int ul, int ur, int x) {
  if (ul <= l && r <= ur) {
    sum[u] = (long long)sum[u] * x % mod;
    tag[u] = (long long)tag[u] * x % mod;
    for (int i = 0; i < (int)facOfMod.size(); ++i) {
      cnt[u][i] += exp[i];
    }
    val[u] = (long long)val[u] * left % mod;
    return;
  }
  pushDown(u);
  int mid = (l + r) >> 1;
  if (ul <= mid) {
    mul(((u) << 1), l, mid, ul, ur, x);
  }
  if (mid < ur) {
    mul(((u) << 1 | 1), mid + 1, r, ul, ur, x);
  }
  pushUp(u);
}
inline void multiply(int l, int r, int x) {
  calcExp(x);
  mul(1, 1, n, l, r, x);
}
void div(int u, int l, int r, int p, int x) {
  if (l == r) {
    for (int i = 0; i < (int)facOfMod.size(); ++i) {
      cnt[u][i] -= exp[i];
    }
    sum[u] = val[u] = (long long)val[u] * power(left, phi - 1) % mod;
    for (int i = 0; i < (int)facOfMod.size(); ++i) {
      sum[u] = (long long)sum[u] * power(facOfMod[i], cnt[u][i]) % mod;
    }
    return;
  }
  pushDown(u);
  int mid = (l + r) >> 1;
  if (p <= mid) {
    div(((u) << 1), l, mid, p, x);
  } else {
    div(((u) << 1 | 1), mid + 1, r, p, x);
  }
  pushUp(u);
}
inline void divide(int p, int x) {
  calcExp(x);
  div(1, 1, n, p, x);
}
int query(int u, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) {
    return sum[u];
  }
  pushDown(u);
  int mid = (l + r) >> 1;
  int res = 0;
  if (ql <= mid) {
    res += query(((u) << 1), l, mid, ql, qr);
    if (res >= mod) {
      res -= mod;
    }
  }
  if (mid < qr) {
    res += query(((u) << 1 | 1), mid + 1, r, ql, qr);
    if (res >= mod) {
      res -= mod;
    }
  }
  pushUp(u);
  return res;
}
}  // namespace Seg
int main() {
  read(n), read(mod);
  modInit();
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  Seg::build(1, 1, n);
  read(q);
  for (int cas = 0, op, l, r, p, x; cas < q; ++cas) {
    read(op);
    if (op == 1) {
      read(l), read(r), read(x);
      Seg::multiply(l, r, x);
    } else if (op == 2) {
      read(p), read(x);
      Seg::divide(p, x);
    } else {
      read(l), read(r);
      printf("%d\n", Seg::query(1, 1, n, l, r));
    }
  }
}
