#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
using namespace std;
clock_t time_p = clock();
void aryanc403() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
class CMP {
 public:
  bool operator()(long long int a, long long int b) { return !(a <= b); }
};
void add(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  map<long long int, long long int>::iterator jt;
  jt = m.find(x);
  if (jt == m.end())
    m.insert(make_pair(x, cnt));
  else
    jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  map<long long int, long long int>::iterator jt;
  jt = m.find(x);
  if (jt->second <= cnt)
    m.erase(jt);
  else
    jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
         const pair<long long int, long long int> &b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
const long long int MaxN = 100000L;
const long long int MaxP = 2000000L;
const long long int MaxF = 10L;
long long int T, n, i, j, k, in, cnt, l, r, mod, ty, x, q, sz;
long long int m;
vector<long long int> a, b;
long long int powBase[MaxF][MaxP + 5];
vector<long long int>::iterator it;
long long int po(long long int a, long long int n) {
  if (n == 0) return 1;
  if (n & 1) return (a * po((a * a) % mod, n / 2)) % mod;
  return po((a * a) % mod, n / 2);
}
long long int euclid(long long int a, long long int b, long long int &x,
                     long long int &y) {
  if (b) {
    long long int d = euclid(b, a % b, y, x);
    return y -= a / b * x, d;
  }
  return x = 1, y = 0, a;
}
long long int modInverse(long long int a) {
  long long int p = mod;
  long long int x, y;
  euclid(a, p, x, y);
  return ((x % p + p) % p);
}
void pre(long long int n) {
  long long int i, j;
  for (i = 2; i * i <= n; ++i) {
    if (n % i == 0) b.push_back(i);
    while (n % i == 0) n /= i;
  }
  if (n > 1) b.push_back(n);
  i = 0;
  for (auto x : b) {
    powBase[i][0] = 1;
    for (j = (1); j <= (MaxP); ++j)
      powBase[i][j] = (powBase[i][j - 1] * x) % mod;
    i++;
  }
}
long long int lazy[4 * MaxN + 5][MaxF], val[4 * MaxN + 5][MaxF];
long long int s[4 * MaxN + 5];
void upd(long long int id, const long long int v[MaxF]) {
  long long int i;
  for (i = 0; i < (sz); ++i) {
    lazy[id][i] += v[i];
    val[id][i] += v[i];
  }
  lazy[id][sz] = (lazy[id][sz] * v[sz]) % mod;
  s[id] = (s[id] * v[sz]) % mod;
}
void shift(long long int id) {
  upd(2 * id, lazy[id]);
  upd(2 * id + 1, lazy[id]);
  long long int i;
  for (i = 0; i < (sz); ++i) lazy[id][i] = 0;
  lazy[id][sz] = 1;
}
void setNode(long long int id) {
  long long int x = s[2 * id], y = s[2 * id + 1], i;
  for (i = 0; i < (sz); ++i) {
    val[id][i] = min(val[2 * id][i], val[2 * id + 1][i]);
    x = (x * powBase[i][val[2 * id][i] - val[id][i]]) % mod;
    y = (y * powBase[i][val[2 * id + 1][i] - val[id][i]]) % mod;
    assert(val[2 * id][i] - val[id][i] <= MaxP);
    assert(val[2 * id + 1][i] - val[id][i] <= MaxP);
  }
  s[id] = (x + y) % mod;
}
void update(long long int id, long long int l, long long int r, long long int L,
            long long int R, const long long int z[MaxF]) {
  if (l > r || r < L || R < l) return;
  if (L <= l && r <= R) {
    upd(id, z);
    return;
  }
  long long int m = (l + r) / 2, i;
  shift(id);
  update(2 * id, l, m, L, R, z);
  update(2 * id + 1, m + 1, r, L, R, z);
  setNode(id);
}
void update(long long int l, long long int r, long long int y,
            long long int fl = 1) {
  long long int cnt = 0, i = 0;
  long long int c[MaxF];
  for (auto x : b) {
    cnt = 0;
    while (y % x == 0) {
      y /= x;
      cnt++;
    }
    c[i] = fl * cnt;
    i++;
  }
  if (fl == 1)
    c[i] = y;
  else
    c[i] = modInverse(y);
  update(1, 1, n, l, r, c);
}
long long int sum(long long int root, long long int l, long long int r,
                  long long int L, long long int R) {
  if (l > r || r < L || R < l) return 0;
  if (L <= l && r <= R) {
    long long int cnt = s[root], i;
    for (i = 0; i < (sz); ++i) cnt = (cnt * powBase[i][val[root][i]]) % mod;
    assert(val[root][i] < MaxP);
    return cnt;
  }
  long long int m = (l + r) / 2;
  shift(root);
  return (sum(2 * root, l, m, L, R) + sum(2 * root + 1, m + 1, r, L, R)) % mod;
}
void dbg(long long int id, long long int l, long long int r) {
  long long int i;
  cerr << id << " id " << l << " " << r << "\n";
  for (i = 0; i < (sz); ++i) cerr << b[i] << " " << val[id][i] << "\n";
  cerr << s[id] << "\n"
       << "\n";
}
void built(long long int id, long long int l, long long int r) {
  long long int i;
  if (l == r) {
    long long int y = a[l];
    for (i = 0; i < (sz); ++i) {
      cnt = 0;
      while (y % b[i] == 0) {
        y /= b[i];
        cnt++;
      }
      val[id][i] = cnt;
      lazy[id][i] = 0;
    }
    s[id] = y % mod;
    lazy[id][sz] = 0;
    return;
  }
  for (i = 0; i < (sz); ++i) {
    lazy[id][i] = 0;
    val[id][i] = 0;
  }
  lazy[id][sz] = 1;
  long long int m = (l + r) / 2;
  built(2 * id, l, m);
  built(2 * id + 1, m + 1, r);
  setNode(id);
  return;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> mod;
  pre(mod);
  sz = (long long int)b.size();
  a.clear();
  a.reserve(n + 1);
  a.push_back(0);
  for (i = (1); i <= (n); ++i) {
    cin >> in;
    a.push_back(in);
  }
  built(1, 1, n);
  cin >> q;
  while (q--) {
    cin >> ty;
    if (ty == 1) {
      cin >> l >> r >> x;
      update(l, r, x);
    } else if (ty == 2) {
      cin >> l >> x;
      update(l, l, x, -1);
    } else {
      cin >> l >> r;
      cout << sum(1, 1, n, l, r) << "\n";
    }
  }
  aryanc403();
  return 0;
}
