#include <bits/stdc++.h>
using namespace std;
int n, m;
long long seed, vmax;
struct node {
  int l, r;
  mutable long long v;
  node(int L, int R = -1, long long V = 0) : l(L), r(R), v(V) {}
  bool operator<(const node& o) const { return l < o.l; }
};
set<node> s;
long long rnd() {
  long long ret = seed;
  seed = (seed * 7 + 13) % 1000000007;
  return ret;
}
long long quickpow(long long a, long long b, long long p) {
  long long res = 1;
  a %= p;
  while (b) {
    if (b & 1) res = (res * a) % p;
    b >>= 1, a = (a * a) % p;
  }
  return res;
}
set<node>::iterator split(int pos) {
  set<node>::iterator it = s.lower_bound(pos);
  if (it != s.end() && it->l == pos) return it;
  --it;
  int L = it->l, R = it->r;
  long long V = it->v;
  s.erase(it);
  s.insert(node(L, pos - 1, V));
  return s.insert(node(pos, R, V)).first;
}
void assign_val(int l, int r, long long v) {
  set<node>::iterator itr = split(r + 1), itl = split(l);
  s.erase(itl, itr);
  s.insert(node(l, r, v));
}
void add(int l, int r, long long x) {
  set<node>::iterator itr = split(r + 1), itl = split(l);
  for (set<node>::iterator it = itl; it != itr; ++it) it->v += x;
}
long long get_k(int l, int r, int k) {
  set<node>::iterator itr = split(r + 1), itl = split(l);
  vector<pair<long long, int> > vec;
  vec.clear();
  for (set<node>::iterator it = itl; it != itr; ++it)
    vec.push_back(make_pair(it->v, it->r - it->l + 1));
  sort(vec.begin(), vec.end());
  for (int i = 0, I = vec.size(); i < I; ++i) {
    k -= vec[i].second;
    if (k <= 0) return vec[i].first;
  }
}
long long getsum(int l, int r, long long x, long long y) {
  set<node>::iterator itr = split(r + 1), itl = split(l);
  long long res = 0;
  for (set<node>::iterator it = itl; it != itr; ++it)
    res = (res + (it->r - it->l + 1) * quickpow(it->v, x, y)) % y;
  return res;
}
int main() {
  cin >> n >> m >> seed >> vmax;
  for (int i = 1; i <= n; ++i) s.insert(node(i, i, rnd() % vmax + 1));
  s.insert(node(n + 1, n + 1, 0));
  int op, l, r, x, y;
  while (m--) {
    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() % vmax) + 1;
    if (op == 4) y = (rnd() % vmax) + 1;
    if (op == 1)
      add(l, r, x);
    else if (op == 2)
      assign_val(l, r, x);
    else if (op == 3)
      printf("%lld\n", get_k(l, r, x));
    else
      printf("%lld\n", getsum(l, r, x, y));
  }
}
