#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r;
  mutable long long value;
  bool operator<(const node &a) const { return l < a.l; }
  node(int L, int R, long long V) : l(L), r(R), value(V) {}
  node(int L) : l(L) {}
};
set<node> s;
set<node>::iterator split(int pos) {
  auto it = s.lower_bound(node(pos));
  if (it != s.end() && it->l == pos) return it;
  --it;
  int l = it->l;
  int r = it->r;
  long long value = it->value;
  s.erase(it);
  s.insert(node(l, pos - 1, value));
  return s.insert(node(pos, r, value)).first;
}
void assign(int l, int r, long long value) {
  auto itr = split(r + 1);
  auto itl = split(l);
  s.erase(itl, itr);
  s.insert(node(l, r, value));
}
void add(int l, int r, long long value) {
  auto itr = split(r + 1);
  auto itl = split(l);
  for (auto it = itl; it != itr; it++) {
    it->value += value;
  }
}
long long kth(int l, int r, int k) {
  auto itr = split(r + 1);
  auto itl = split(l);
  vector<pair<long long, int> > lst;
  for (auto it = itl; it != itr; it++) {
    lst.emplace_back(it->value, it->r - it->l + 1);
  }
  sort(lst.begin(), lst.end());
  for (auto [value, cnt] : lst) {
    k -= cnt;
    if (k <= 0) return value;
  }
  return -1;
}
long long power(long long q, long long w, long long mod) {
  long long ret = 1;
  q %= mod;
  while (w) {
    if (w & 1) ret = ret * q % mod;
    q = q * q % mod;
    w >>= 1;
  }
  return ret;
}
long long range_sum(int l, int r, int x, int y) {
  auto itr = split(r + 1);
  auto itl = split(l);
  long long res = 0;
  for (auto it = itl; it != itr; it++) {
    res = (res + (it->r - it->l + 1) * power(it->value, x, y)) % y;
  }
  return res;
}
int n, m, seed, vmax;
int rng() {
  int ret = seed;
  seed = (7ll * seed + 13) % (1000000007);
  return ret;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> seed >> vmax;
  for (int i = 1; i <= n; i += 1) {
    int x = rng() % vmax + 1;
    s.insert(node(i, i, x));
  }
  for (int i = 1; i <= m; i += 1) {
    int op = rng() % 4 + 1;
    int l = rng() % n + 1;
    int r = rng() % n + 1;
    if (l > r) swap(l, r);
    int x, y;
    if (op == 3) {
      x = (rng() % (r - l + 1)) + 1;
    } else {
      x = rng() % vmax + 1;
    }
    if (op == 4) {
      y = rng() % vmax + 1;
    }
    if (op == 1) {
      add(l, r, x);
    }
    if (op == 2) {
      assign(l, r, x);
    }
    if (op == 3) {
      cout << kth(l, r, x) << '\n';
    }
    if (op == 4) {
      cout << range_sum(l, r, x, y) << "\n";
    }
  }
  return 0;
}
