#include <bits/stdc++.h>
using namespace std;
int read() {
  int ret = 0, neg = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return ret * neg;
}
struct Node {
  int l, r;
  mutable long long val;
  Node(int L, int R, long long Val) : l(L), r(R), val(Val) {}
};
bool operator<(Node x, Node y) { return x.l < y.l; }
set<Node> s;
set<Node>::iterator split(int pos) {
  set<Node>::iterator it = s.lower_bound(Node(pos, -1, 0));
  if (it != s.end() && it->l == pos) return it;
  it--;
  long long val = it->val;
  int l = it->l, r = it->r;
  s.erase(it);
  s.insert(Node(l, pos - 1, val));
  return s.insert(Node(pos, r, val)).first;
}
void merge(int l, int r, long long val) {
  set<Node>::iterator it1 = split(l);
  set<Node>::iterator it2 = split(r + 1);
  s.erase(it1, it2);
  s.insert(Node(l, r, val));
}
void add(int l, int r, long long val) {
  set<Node>::iterator it1 = split(l);
  set<Node>::iterator it2 = split(r + 1);
  for (; it1 != it2; it1++) it1->val += val;
}
long long rankk(int l, int r, int k) {
  set<Node>::iterator it1 = split(l);
  set<Node>::iterator it2 = split(r + 1);
  vector<pair<long long, int> > vt;
  for (; it1 != it2; it1++)
    vt.push_back(make_pair(it1->val, it1->r - it1->l + 1));
  sort(vt.begin(), vt.end());
  int len = vt.size();
  for (int i = 0; i < len; i++) {
    k -= vt[i].second;
    if (k <= 0) return vt[i].first;
  }
}
long long poww(long long a, long long b, long long mo) {
  long long ret = 1;
  a = a % mo;
  while (b) {
    if (b & 1) ret = ret * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return ret;
}
long long query(int l, int r, int x, long long y) {
  long long ret = 0;
  set<Node>::iterator it1 = split(l);
  set<Node>::iterator it2 = split(r + 1);
  for (; it1 != it2; it1++)
    ret =
        (ret + poww(it1->val, x, y) * (long long)(it1->r - it1->l + 1) % y) % y;
  return ret;
}
int n, m;
long long seed, vmax;
long long rnd() {
  long long ret = seed;
  seed = (seed * 7 + 13) % 1000000007;
  return ret;
}
int main() {
  n = read();
  m = read();
  seed = read();
  vmax = read();
  for (int i = 1; i <= n; i++) s.insert(Node(i, i, (rnd() % vmax) + 1));
  s.insert(Node(n + 1, n + 1, 0));
  int ope, l, r, x, y;
  for (int i = 1; i <= m; i++) {
    ope = rnd() % 4 + 1;
    l = rnd() % n + 1;
    r = rnd() % n + 1;
    if (l > r) swap(l, r);
    if (ope == 3)
      x = (rnd() % (r - l + 1)) + 1;
    else
      x = (rnd() % vmax) + 1;
    if (ope == 4) y = (rnd() % vmax) + 1;
    if (ope == 1) add(l, r, (long long)x);
    if (ope == 2) merge(l, r, (long long)x);
    if (ope == 3) printf("%lld\n", rankk(l, r, x));
    if (ope == 4) printf("%lld\n", query(l, r, x, (long long)y));
  }
}
