#include <bits/stdc++.h>
using namespace std;
long long seed, vmax;
long long rnd() {
  long long res = seed;
  seed = (seed * 7 + 13) % 1000000007;
  return res;
}
long long n, m;
long long a[1000001];
struct node {
  long long l, r;
  mutable long long val;
  node(long long _l = 0, long long _r = -1, long long _v = 0) {
    l = _l;
    r = _r;
    val = _v;
  }
};
bool operator<(const node &a, const node &b) { return a.l < b.l; }
set<node> st;
long long qpow(long long a, long long b, long long p) {
  long long res = 1;
  a %= p;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1) res = res * a % p;
  return res;
}
set<node>::iterator split(long long pos) {
  set<node>::iterator it = st.lower_bound(node(pos));
  if (it != st.end() && it->l == pos) return it;
  --it;
  long long l = it->l, r = it->r;
  long long val = it->val;
  st.erase(it);
  st.insert(node(l, pos - 1, val));
  return st.insert(node(pos, r, val)).first;
}
void assign(long long l, long long r, long long val) {
  set<node>::iterator end = split(r + 1), beg = split(l);
  st.erase(beg, end);
  st.insert(node(l, r, val));
}
void add(long long l, long long r, long long val) {
  set<node>::iterator end = split(r + 1), beg = split(l);
  for (; beg != end; ++beg) beg->val += val;
}
long long getkth(long long l, long long r, long long k) {
  vector<pair<long long, long long> > vec;
  set<node>::iterator end = split(r + 1), beg = split(l);
  for (; beg != end; ++beg)
    vec.push_back(pair<long long, long long>(beg->val, beg->r - beg->l + 1));
  sort(vec.begin(), vec.end());
  for (vector<pair<long long, long long> >::iterator it = vec.begin();
       it != vec.end(); ++it) {
    k -= it->second;
    if (k <= 0) return it->first;
  }
  return -1;
}
long long getsum(long long l, long long r, long long x, long long y) {
  long long res = 0;
  set<node>::iterator end = split(r + 1), beg = split(l);
  for (; beg != end; ++beg)
    res = (res + qpow(beg->val, x, y) * (beg->r - beg->l + 1) % y) % y;
  return res;
}
signed main() {
  scanf("%lld%lld%lld%lld", &n, &m, &seed, &vmax);
  for (long long i = 1; i <= n; ++i) {
    a[i] = (rnd() % vmax) + 1;
    st.insert(node(i, i, a[i]));
  }
  long long op, l, r;
  long long x, y;
  for (long long i = 1; 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() % vmax) + 1;
    if (op == 4) y = (rnd() % vmax) + 1;
    if (op == 1)
      add(l, r, x);
    else if (op == 2)
      assign(l, r, x);
    else if (op == 3)
      printf("%lld\n", getkth(l, r, x));
    else if (op == 4)
      printf("%lld\n", getsum(l, r, x, y));
  }
}
