#include <bits/stdc++.h>
using namespace std;
long long n, m, Seed, Vmax;
long long Rnd() {
  long long ret = Seed;
  Seed = (Seed * 7 + 13) % 1000000007;
  return ret;
}
struct ODT {
  long long l, r;
  mutable long long v;
  bool operator<(const ODT &b) const { return l < b.l; }
};
set<ODT> s;
set<ODT>::iterator Split(long long pos) {
  set<ODT>::iterator it = s.lower_bound((ODT){pos, pos, -1});
  if (it != s.end() && it->l == pos) return it;
  it--;
  ODT ins = *it;
  s.erase(it), s.insert((ODT){ins.l, pos - 1, ins.v});
  return s.insert((ODT){pos, ins.r, ins.v}).first;
}
void Assign(long long l, long long r, long long v) {
  set<ODT>::iterator L = Split(l), R = Split(r + 1);
  s.erase(L, R), s.insert((ODT){l, r, v});
}
long long Fast_Power(long long a, long long b, long long mod) {
  a %= mod;
  long long sum = 1;
  while (b) {
    if (b & 1) sum *= a, sum %= mod;
    a *= a, a %= mod, b >>= 1;
  }
  return sum;
}
long long Calc(long long l, long long r, long long x, long long y) {
  set<ODT>::iterator L = Split(l), R = Split(r + 1);
  long long ans = 0;
  for (set<ODT>::iterator it = L; it != R; it++)
    ans += (long long)(it->r - it->l + 1) * Fast_Power(it->v, x, y), ans %= y;
  return ans;
}
void Modify(long long l, long long r, long long x) {
  set<ODT>::iterator L = Split(l), R = Split(r + 1);
  for (set<ODT>::iterator it = L; it != R; it++) it->v += x;
}
struct Node {
  long long len, v;
};
bool cmp(Node a, Node b) { return a.v < b.v; }
long long Find_Kth(long long l, long long r, long long k) {
  vector<Node> q;
  q.clear();
  set<ODT>::iterator L = Split(l), R = Split(r + 1);
  for (set<ODT>::iterator it = L; it != R; it++)
    q.push_back((Node){it->r - it->l + 1, it->v});
  sort(q.begin(), q.end(), cmp);
  long long sum = 0;
  for (vector<Node>::iterator it = q.begin(); it != q.end(); it++) {
    sum += it->len;
    if (sum >= k) return it->v;
  }
  return -1;
}
long long a[100010];
int main() {
  cin >> n >> m >> Seed >> Vmax;
  for (long long i = 1; i <= n; i++) a[i] = Rnd() % Vmax + 1;
  for (int i = 1; i <= n; i++) s.insert((ODT){i, i, a[i]});
  s.insert((ODT){n + 1, n + 1, 0});
  for (long long i = 1; i <= m; i++) {
    long long opt = (Rnd() % 4) + 1, l = (Rnd() % n) + 1, r = (Rnd() % n) + 1,
              x, y;
    if (l > r) swap(l, r);
    if (opt == 3)
      x = (Rnd() % (r - l + 1)) + 1;
    else
      x = (Rnd() % Vmax) + 1;
    if (opt == 4) y = (Rnd() % Vmax) + 1;
    if (opt == 1)
      Modify(l, r, x);
    else if (opt == 2)
      Assign(l, r, x);
    else if (opt == 3)
      printf("%I64d\n", Find_Kth(l, r, x));
    else if (opt == 4)
      printf("%I64d\n", Calc(l, r, x, y));
  }
}
