#include <bits/stdc++.h>
using namespace std;
int n, m, seed, vmax;
long long arr[100000];
map<long long, int> mp;
int rnd() {
  int ret = seed;
  seed = (seed * 7LL + 13) % 1000000007;
  return ret;
}
template <typename T>
struct BLOCK {
  int nb, mb, mbi;
  T *arr, *upd;
  int *st, *ed;
  typedef void (*BLOCK_UP)(T &, const T &v);
  typedef void (*BLOCK_QR)(const T &u, int l, int r);
  BLOCK::BLOCK_UP upp;
  BLOCK(T *arr, int n, typename BLOCK::BLOCK_UP upp) {
    for (mb = 1, mbi = 0; mb < 256 && mb * mb < n; mb <<= 1, mbi++)
      ;
    nb = n >> mbi;
    if ((n & (mb - 1)) != 0) nb++;
    st = new int[nb];
    ed = new int[nb];
    for (int i = 0; i < nb; i++) st[i] = i << mbi, ed[i] = st[i] + mb;
    if ((n & (mb - 1)) != 0) ed[nb - 1] = st[nb - 1] + (n & (mb - 1));
    upd = new T[nb];
    memset(upd, 0, sizeof(T) * nb);
    this->arr = arr;
    this->upp = upp;
  }
  ~BLOCK() { delete[] upd, delete[] st, delete[] ed; }
  void push(int ib) {
    for (int i = st[ib], ii = ed[ib]; i < ii; i++) upp(arr[i], upd[ib]);
    upd[ib] = 0;
  }
  void update(int l, int r, typename BLOCK::BLOCK_UP upx,
              typename BLOCK::BLOCK_UP upr, const T &v) {
    int il = l >> mbi, ir = r >> mbi, irr = (r - 1) >> mbi;
    if (il == irr) {
      push(il);
      for (int i = l; i < r; i++) upx(arr[i], v);
    } else {
      if ((l & (mb - 1)) != 0) {
        push(il);
        for (int i = l, ii = ed[il]; i < ii; i++) upx(arr[i], v);
        il++;
      }
      if ((r & (mb - 1)) != 0) {
        push(ir);
        for (int i = st[ir]; i < r; i++) upx(arr[i], v);
      }
      for (int i = il, j = ir; i < j; i++) upr(upd[i], v);
    }
  }
  void query(int l, int r, typename BLOCK::BLOCK_QR qr) {
    int il = l >> mbi, ir = r >> mbi, irr = (r - 1) >> mbi;
    if (il == irr)
      qr(upd[il], l, r);
    else {
      if ((l & (mb - 1)) != 0) qr(upd[il], l, ed[il]), il++;
      if ((r & (mb - 1)) != 0) qr(upd[ir], st[ir], r);
      for (int i = il, j = ir; i < j; i++) qr(upd[i], st[i], ed[i]);
    }
  }
};
void alter_count(long long k, int i) { mp.count(k) ? mp[k] += i : mp[k] = i; }
long long power(long long x, int n, int y) {
  long long r = (n & 1) ? x : 1, rr = n == 1 ? 1 : power(x, n >> 1, y);
  return r * rr % y * rr % y;
}
void upp(long long &x, const long long &v) { v >= 0 ? x += v : x = -v; }
void upx0(long long &x, const long long &v) { x += v; }
void upr0(long long &x, const long long &v) { x >= 0 ? x += v : x = -(-x + v); }
void upx1(long long &x, const long long &v) { x = v; }
void upr1(long long &x, const long long &v) { x = -v; }
void qr(const long long &t, int l, int r) {
  if (t >= 0)
    for (int i = l; i < r; i++) alter_count(arr[i] + t, 1);
  else
    alter_count(-t, r - l);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> seed >> vmax;
  for (int i = 0; i < n; i++) arr[i] = rnd() % vmax + 1;
  BLOCK<long long> block(arr, n, upp);
  for (int iq = 0; iq < m; iq++) {
    int op = rnd() % 4;
    int l = rnd() % n, r = rnd() % n, x, y = 0;
    if (l > r) swap(l, r);
    r++;
    if (op == 2)
      x = rnd() % (r - l) + 1;
    else
      x = rnd() % vmax + 1;
    if (op == 3) y = rnd() % vmax + 1;
    if (op == 0)
      block.update(l, r, upx0, upr0, x);
    else if (op == 1)
      block.update(l, r, upx1, upr1, x);
    else if (op == 2 || op == 3) {
      mp.clear();
      block.query(l, r, qr);
      long long ans = 0;
      if (op == 2) {
        ans = -1;
        for (auto &r : mp) {
          if (r.second >= x) {
            ans = r.first;
            break;
          } else
            x -= r.second;
        }
      } else {
        for (auto &r : mp)
          ans = (ans + power(r.first % y, x, y) * r.second % y) % y;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
