#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
struct cmp {
  bool operator()(const pair<int, long long>& a,
                  const pair<int, long long>& b) {
    return a.second > b.second;
  }
};
const int maxn = 2e5 + 5;
const long long M = 1e9 + 7;
long long a[maxn];
int n, m;
long long seed, mod;
long long rd() {
  long long ret = seed;
  seed = (seed * 7 + 13) % M;
  return ret;
}
struct Node {
  int l, r;
  mutable long long v;
  Node(int lp, int rr = -1, long long vv = 0) : l(lp), r(rr), v(vv){};
  bool operator<(const Node& a) const { return l < a.l; }
};
set<Node> ss;
set<Node>::iterator split(int pos) {
  set<Node>::iterator it = ss.lower_bound(Node(pos));
  if (it != ss.end() && it->l == pos) {
    return it;
  }
  --it;
  int l = it->l;
  int r = it->r;
  long long v = it->v;
  ss.erase(it);
  ss.insert(Node(l, pos - 1, v));
  return ss.insert(Node(pos, r, v)).first;
}
void change(int l, int r, long long num) {
  set<Node>::iterator it = split(r + 1);
  set<Node>::iterator it2 = split(l);
  ss.erase(it2, it);
  ss.insert(Node(l, r, num));
}
void add(int l, int r, long long num) {
  set<Node>::iterator itr = split(r + 1);
  set<Node>::iterator itl = split(l);
  for (; itl != itr; ++itl) itl->v += num;
}
bool cmp(pair<int, long long> a, pair<int, long long> b) {
  return a.second < b.second;
}
long long find_k(int l, int r, int k) {
  std::vector<pair<int, long long> > vv;
  set<Node>::iterator itr = split(r + 1);
  set<Node>::iterator itl = split(l);
  while (itl != itr) {
    vv.push_back(make_pair(itl->r - itl->l + 1, itl->v));
    itl++;
  }
  sort(vv.begin(), vv.end(), cmp);
  int s = 0;
  for (int i = 0; i < vv.size(); i++) {
    s += vv[i].first;
    if (s >= k) return vv[i].second;
  }
  return -1;
}
long long power(long long a, long long b, long long m) {
  a %= m;
  long long ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a % m;
    }
    a = a * a % m;
    b >>= 1;
  }
  return ans;
}
long long gets(int l, int r, long long a, long long b) {
  set<Node>::iterator itr = split(r + 1);
  set<Node>::iterator itl = split(l);
  long long ans = 0;
  for (; itl != itr; itl++) {
    ans = (ans + (itl->r - itl->l + 1) * power(itl->v, a, b) % b) % b;
  }
  return ans;
}
int main() {
  scanf("%d%d%lld%lld", &n, &m, &seed, &mod);
  for (int i = 1; i <= n; i++) {
    a[i] = (rd() % mod) + 1;
    ss.insert(Node(i, i, a[i]));
  }
  ss.insert(Node(n + 1, n + 1, 0));
  while (m--) {
    int f = rd() % 4 + 1;
    int l = (rd() % n) + 1;
    int r = (rd() % n) + 1;
    if (l > r) swap(l, r);
    int x, y;
    if (f == 1) {
      x = (rd() % mod) + 1;
      add(l, r, x);
    } else if (f == 2) {
      x = (rd() % mod) + 1;
      change(l, r, x);
    } else if (f == 3) {
      x = (rd() % (r - l + 1)) + 1;
      long long s = find_k(l, r, x);
      printf("%lld\n", s);
    } else {
      x = (rd() % mod) + 1;
      y = (rd() % mod) + 1;
      long long ans = gets(l, r, x, y);
      printf("%lld\n", ans % y);
    }
  }
}
