#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int N = 1e5 + 5;
int q, type, l, r, x, pos;
long long n, a[N], tmp, mod, cnt, check[N], prime[N];
long long st[4 * N], lazy[4 * N], lazy_pr[4 * N][10], dd[4 * N];
long long pw[10][17 * N];
vector<int> uoc;
map<int, int> idx;
void init() {
  check[1] = 1;
  for (int i = 2; i * i <= 1e5; i++) {
    if (check[i] == 0) {
      for (int j = i * i; j <= 1e5; j = j + i) {
        check[j] = 1;
      }
    }
  }
  for (int i = 2; i <= 1e5; i++) {
    if (check[i] == 0) {
      cnt++;
      prime[cnt] = i;
    }
  }
}
pair<long long, long long> revmod(long long a, long long m) {
  if (a == 0) {
    return {0, 1};
  }
  pair<long long, long long> tmp = revmod(m % a, a);
  return {tmp.second - (m / a) * tmp.first, tmp.first};
}
void push_down(int id, int l, int r) {
  if (lazy[id] != 1) {
    st[id] = (st[id] * lazy[id]) % mod;
    if (l < r) {
      lazy[id << 1] = (lazy[id << 1] * lazy[id]) % mod;
      lazy[id << 1 | 1] = (lazy[id << 1 | 1] * lazy[id]) % mod;
    }
    lazy[id] = 1;
  }
  if (l < r) {
    if (dd[id] == 1) {
      for (int i = 0; i < uoc.size(); i++) {
        if (lazy_pr[id][i + 1] != 0) {
          st[id] = (st[id] * pw[i][lazy_pr[id][i + 1]]) % mod;
          lazy_pr[id << 1][i + 1] += lazy_pr[id][i + 1];
          lazy_pr[id << 1 | 1][i + 1] += lazy_pr[id][i + 1];
          lazy_pr[id][i + 1] = 0;
        }
      }
      dd[id << 1] = dd[id << 1 | 1] = 1;
      dd[id] = 0;
    }
  }
}
long long val(int id, int l, int r) {
  if (l < r) {
    return st[id];
  } else {
    long long tmp = st[id];
    for (int i = 0; i < uoc.size(); i++) {
      if (lazy_pr[id][i + 1] != 0) {
        tmp = (tmp * pw[i][lazy_pr[id][i + 1]]) % mod;
      }
    }
    return tmp;
  }
}
void update(int id, int l, int r, int i, int j, long long x) {
  push_down(id, l, r);
  if (l > j || r < i || i > j) {
    return;
  }
  if (l >= i && r <= j) {
    st[id] = (st[id] * x) % mod;
    if (l < r) {
      lazy[id << 1] = (lazy[id << 1] * x) % mod;
      lazy[id << 1 | 1] = (lazy[id << 1 | 1] * x) % mod;
    }
    return;
  }
  int mid = (l + r) >> 1;
  update(id << 1, l, mid, i, j, x);
  update(id << 1 | 1, mid + 1, r, i, j, x);
  st[id] = (val(id << 1, l, mid) + val(id << 1 | 1, mid + 1, r)) % mod;
}
void update_pr(int id, int l, int r, int i, int j, int x, int pos) {
  push_down(id, l, r);
  if (l > j || r < i || i > j) {
    return;
  }
  if (l >= i && r <= j) {
    if (l < r) {
      st[id] = (st[id] * pw[pos - 1][x]) % mod;
      lazy_pr[id << 1][pos] += x;
      lazy_pr[id << 1 | 1][pos] += x;
      dd[id << 1] = dd[id << 1 | 1] = 1;
      return;
    } else {
      lazy_pr[id][pos] += x;
      return;
    }
  }
  int mid = (l + r) >> 1;
  update_pr(id << 1, l, mid, i, j, x, pos);
  update_pr(id << 1 | 1, mid + 1, r, i, j, x, pos);
  st[id] = (val(id << 1, l, mid) + val(id << 1 | 1, mid + 1, r)) % mod;
}
long long get(int id, int l, int r, int i, int j) {
  push_down(id, l, r);
  if (l > j || r < i || i > j) {
    return 0;
  }
  if (l >= i && r <= j) {
    return val(id, l, r);
  }
  int mid = (l + r) >> 1;
  return (get(id << 1, l, mid, i, j) + get(id << 1 | 1, mid + 1, r, i, j)) %
         mod;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> mod;
  for (int i = 1; i <= n; i++) cin >> a[i];
  init();
  tmp = mod;
  for (int i = 2; i <= sqrt(tmp); i++) {
    if (tmp % i == 0) {
      uoc.push_back(i);
      idx[i] = uoc.size();
      while (tmp % i == 0) tmp = tmp / i;
    }
  }
  if (tmp > 1) {
    uoc.push_back(tmp);
    idx[tmp] = uoc.size();
  }
  for (int i = 0; i < uoc.size(); i++) {
    pw[i][0] = 1;
    for (int j = 1; j <= 17 * 1e5; j++) {
      pw[i][j] = (pw[i][j - 1] * uoc[i]) % mod;
    }
  }
  for (int i = 1; i <= 4 * n + 1; i++) {
    st[i] = lazy[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    long long hah = 1;
    for (int j = 1; prime[j] <= sqrt(a[i]); j++) {
      if (a[i] % prime[j] == 0) {
        int dem = 0, uwu = 1;
        while (a[i] % prime[j] == 0) {
          a[i] = a[i] / prime[j];
          dem++;
          uwu = uwu * prime[j];
        }
        if (idx[prime[j]] > 0) {
          update_pr(1, 1, n, i, i, dem, idx[prime[j]]);
        } else {
          hah = hah * uwu;
        }
      }
    }
    if (a[i] > 1) {
      if (idx[a[i]] > 0) {
        update_pr(1, 1, n, i, i, 1, idx[a[i]]);
      } else {
        hah = hah * a[i];
      }
    }
    update(1, 1, n, i, i, hah);
  }
  cin >> q;
  for (int vl = 1; vl <= q; vl++) {
    cin >> type;
    if (type == 1) {
      cin >> l >> r >> x;
      long long hah = 1;
      for (int i = 1; prime[i] <= sqrt(x); i++) {
        if (x % prime[i] == 0) {
          int dem = 0, uwu = 1;
          while (x % prime[i] == 0) {
            x = x / prime[i];
            dem++;
            uwu = uwu * prime[i];
          }
          if (idx[prime[i]] > 0) {
            update_pr(1, 1, n, l, r, dem, idx[prime[i]]);
          } else {
            hah = hah * uwu;
          }
        }
      }
      if (x > 1) {
        if (idx[x] > 0) {
          update_pr(1, 1, n, l, r, 1, idx[x]);
        } else {
          hah = hah * x;
        }
      }
      update(1, 1, n, l, r, hah);
    }
    if (type == 2) {
      cin >> pos >> x;
      long long hah = 1;
      for (int i = 1; prime[i] <= sqrt(x); i++) {
        if (x % prime[i] == 0) {
          int dem = 0, uwu = 1;
          while (x % prime[i] == 0) {
            x = x / prime[i];
            dem++;
            uwu = uwu * prime[i];
          }
          if (idx[prime[i]] > 0) {
            update_pr(1, 1, n, pos, pos, -dem, idx[prime[i]]);
          } else {
            hah = hah * uwu;
          }
        }
      }
      if (x > 1) {
        if (idx[x] > 0) {
          update_pr(1, 1, n, pos, pos, -1, idx[x]);
        } else {
          hah = hah * x;
        }
      }
      pair<long long, long long> tmp = revmod(hah, mod);
      hah = (tmp.first + mod * mod) % mod;
      update(1, 1, n, pos, pos, hah);
    }
    if (type == 3) {
      cin >> l >> r;
      cout << get(1, 1, n, l, r) << "\n";
    }
  }
}
