#include <bits/stdc++.h>
using namespace std;
int n, m, seed, vm;
struct node {
  int l, r;
  mutable long long v;
  node() {}
  node(int a, int b, long long c) {
    l = a;
    r = b;
    v = c;
  }
  bool operator<(node b) const { return l < b.l; }
};
set<node> s;
long long qpow(long long x, long long k, long long mod) {
  long long res = 1;
  while (k) {
    if (k & 1) res = res * x % mod;
    x = x * x % mod;
    k >>= 1;
  }
  return res;
}
int rnd() {
  int ret = seed;
  seed = (seed * 7ll + 13) % 1000000007;
  return ret;
}
set<node>::iterator split(int pos) {
  set<node>::iterator p = s.upper_bound(node(pos, 0, 0));
  p--;
  if (p->l == pos) return p;
  int L = p->l, R = p->r;
  long long V = p->v;
  s.erase(p);
  s.insert(node(L, pos - 1, V));
  return s.insert(node(pos, R, V)).first;
}
void cover(int l, int r, int val) {
  set<node>::iterator lp = split(l), rp = split(r + 1);
  s.erase(lp, rp);
  s.insert(node(l, r, val));
}
void add(int l, int r, int val) {
  set<node>::iterator lp = split(l), rp = split(r + 1);
  for (; lp != rp; lp->v += val, lp++)
    ;
}
long long rk(int l, int r, int k) {
  map<long long, int> mp;
  mp.clear();
  set<node>::iterator lp = split(l), rp = split(r + 1);
  for (; lp != rp; lp++) mp[lp->v] += lp->r - lp->l + 1;
  map<long long, int>::iterator mit;
  pair<long long, int> p;
  mit = mp.begin();
  for (; mit != mp.end(); mit++) {
    p = *mit;
    if (k <= p.second)
      return p.first;
    else
      k -= p.second;
  }
  return -1ll;
}
long long query(int l, int r, int k, int mod) {
  set<node>::iterator lp = split(l), rp = split(r + 1);
  long long res = 0;
  for (; lp != rp; lp++)
    res = (res + (lp->r - lp->l + 1) * qpow(lp->v % mod, k, mod) % mod) % mod;
  return res;
}
inline int read() {
  int res = 0;
  char ch = getchar(), ch1 = ch;
  while (!isdigit(ch)) ch1 = ch, ch = getchar();
  while (isdigit(ch)) res = (res << 3) + (res << 1) + ch - '0', ch = getchar();
  return ch1 == '-' ? -res : res;
}
int main() {
  n = read();
  m = read();
  seed = read();
  vm = read();
  for (register int i = 1; i <= n; ++i) s.insert(node(i, i, (rnd() % vm) + 1));
  s.insert(node(n + 1, n + 1, 0));
  for (register int i = 1, op, l, r, x, y; i <= m; ++i) {
    op = (rnd() % 4) + 1;
    l = (rnd() % n) + 1;
    r = (rnd() % n) + 1;
    if (l > r) swap(l, r);
    if (op == 3)
      x = (rnd() % (r - l + 1)) + 1;
    else
      x = (rnd() % vm) + 1;
    if (op == 4) y = (rnd() % vm) + 1;
    if (op == 1)
      add(l, r, x);
    else if (op == 2)
      cover(l, r, x);
    else if (op == 3)
      printf("%lld\n", rk(l, r, x));
    else
      printf("%lld\n", query(l, r, x, y));
  }
  return 0;
}
