#include <bits/stdc++.h>
int seed;
using LL = long long;
inline int rnd() {
  int ret = seed;
  seed = (static_cast<LL>(7) * seed + 13) % 1000000007;
  return ret;
}
namespace intervals {
struct INVL {
  int L;
  mutable LL val;
  inline bool operator<(const INVL& RHS) const { return L < RHS.L; }
};
std::set<INVL> invl;
inline void init(int i, int ai) { invl.insert(invl.end(), {i, ai}); }
using SII = std::set<INVL>::iterator;
inline SII split_get(int L) {
  SII pos = --invl.upper_bound({L, std::numeric_limits<LL>::max()});
  return pos->L < L ? invl.insert(pos, {L, pos->val}) : pos;
}
inline void add(int L, int R, int x) {
  std::for_each(split_get(L), split_get(R), [x](const INVL& i) { i.val += x; });
}
inline void set(int L, int R, int x) {
  invl.erase(split_get(L), split_get(R)), invl.insert({L, x});
}
std::pair<LL, int> _[100000];
inline LL select(int L, int R, int k) {
  SII iL = split_get(L);
  std::sort(_, std::transform(iL, split_get(R), std::next(iL), _,
                              [](const INVL& cur, const INVL& next) {
                                return std::make_pair(cur.val, next.L - cur.L);
                              }));
  auto* i = _;
  while (k > i->second) k -= i->second, ++i;
  return i->first;
}
inline int pow_sum(int L, int R, int x, int y) {
  LL res = 0;
  for (SII iL = split_get(L), iR = split_get(R); iL != iR; ++iL) {
    int R = 1;
    const auto mul = [y](int a, int b) { return static_cast<LL>(a) * b % y; };
    for (int B = iL->val % y, E = x; E; E >>= 1, B = mul(B, B))
      if (E & 1) R = mul(R, B);
    res += mul(std::next(iL)->L - iL->L, R);
  }
  return res % y;
}
}  // namespace intervals
int main() {
  int n, m, vmax;
  std::cin >> n >> m >> seed >> vmax;
  for (int i = 0; i < n; ++i) intervals::init(i, rnd() % vmax + 1);
  intervals::init(n, 0);
  while (m--) {
    int op = rnd() & 3, l = rnd() % n, r = rnd() % n;
    if (l > r) std::swap(l, r);
    ++r;
    int x = rnd() % (op == 2 ? r - l : vmax) + 1;
    switch (op) {
      case 0:
        intervals::add(l, r, x);
        break;
      case 1:
        intervals::set(l, r, x);
        break;
      case 2:
        std::cout << intervals::select(l, r, x) << '\n';
        break;
      case 3:
        std::cout << intervals::pow_sum(l, r, x, rnd() % vmax + 1) << '\n';
    }
  }
}
