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