Problem: — Willem...

— What's the matter?

— It seems that there's something wrong with Seniorious...

— I'll have a look...

<image>

Seniorious is made by linking special talismans in particular order.

After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.

Seniorious has n pieces of talisman. Willem puts them in a line, the i-th of which is an integer ai.

In order to maintain it, Willem needs to perform m operations.

There are four types of operations:

  * 1 l r x: For each i such that l ≤ i ≤ r, assign ai + x to ai.
  * 2 l r x: For each i such that l ≤ i ≤ r, assign x to ai.
  * 3 l r x: Print the x-th smallest number in the index range [l, r], i.e. the element at the x-th position if all the elements ai such that l ≤ i ≤ r are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1 ≤ x ≤ r - l + 1.
  * 4 l r x y: Print the sum of the x-th power of ai such that l ≤ i ≤ r, modulo y, i.e. <image>.

Input

The only line contains four integers n, m, seed, vmax (1 ≤ n, m ≤ 105, 0 ≤ seed < 109 + 7, 1 ≤ vmax ≤ 109).

The initial values and operations are generated using following pseudo code:
    
    
      
    def rnd():  
      
        ret = seed  
        seed = (seed * 7 + 13) mod 1000000007  
        return ret  
      
    for i = 1 to n:  
      
        a[i] = (rnd() mod vmax) + 1  
      
    for i = 1 to m:  
      
        op = (rnd() mod 4) + 1  
        l = (rnd() mod n) + 1  
        r = (rnd() mod n) + 1  
      
        if (l > r):   
             swap(l, r)  
      
        if (op == 3):  
            x = (rnd() mod (r - l + 1)) + 1  
        else:  
            x = (rnd() mod vmax) + 1  
      
        if (op == 4):  
            y = (rnd() mod vmax) + 1  
      
    

Here op is the type of the operation mentioned in the legend.

Output

For each operation of types 3 or 4, output a line containing the answer.

Examples

Input

10 10 7 9


Output

2
1
0
3


Input

10 10 9 9


Output

1
1
3
3

Note

In the first example, the initial array is {8, 9, 7, 2, 3, 1, 5, 6, 4, 8}.

The operations are:

  * 2 6 7 9
  * 1 3 10 8
  * 4 4 6 2 4
  * 1 4 5 8
  * 2 1 7 1
  * 4 7 9 4 4
  * 1 2 7 9
  * 4 5 8 1 1
  * 2 5 7 5
  * 4 3 10 8 5

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T power(T a, long long int b) {
  T twoPow = a;
  T rv(1);
  while (b > 0) {
    if (b & 1LL) rv *= twoPow;
    twoPow *= twoPow;
    b >>= 1;
  }
  return rv;
}
template <typename T, typename Op>
T ah_power(T a, long long int b, const T& unit_t, Op op) {
  T two_pow = a;
  T ret = unit_t;
  while (b > 0) {
    if (b & 1LL) ret = op(ret, two_pow);
    two_pow = op(two_pow, two_pow);
    b >>= 1;
  }
  return ret;
}
tuple<long long int, long long int, long long int> mut_div(long long int a,
                                                           long long int b,
                                                           long long int c,
                                                           bool eff_c = true) {
  if (a == 0) {
    if (eff_c) {
      if (c % b != 0)
        return {-1, -1, -1};
      else
        return {abs(b), 0, c / b};
    } else {
      if (b < 0)
        return {-b, 0, -1};
      else
        return {b, 0, 1};
    }
  } else {
    auto [g, t, u] = mut_div(b % a, a, c, eff_c);
    if (g == -1) return {-1, -1, -1};
    return {g, u - (b / a) * t, t};
  }
}
tuple<long long int, long long int, long long int> eGCD(long long int a,
                                                        long long int b) {
  return mut_div(a, b, 0, false);
}
pair<long long int, long long int> crt_sub(long long int a1, long long int x1,
                                           long long int a2, long long int x2) {
  a1 = a1 % x1;
  a2 = a2 % x2;
  auto [g, s, t] = mut_div(x1, -x2, a2 - a1);
  if (g == -1) return {-1, -1};
  long long int z = x1 / g * x2;
  s = s % (x2 / g);
  long long int r = (x1 * s + a1) % z;
  if (r < 0) r += z;
  return {r, z};
};
long long int crt(long long int a1, long long int x1, long long int a2,
                  long long int x2) {
  return crt_sub(a1, x1, a2, x2).first;
}
long long int crt(vector<long long int> as, vector<long long int> xs) {
  assert(xs.size() == as.size() && xs.size() > 0);
  long long int r = as[0];
  long long int z = xs[0];
  for (size_t i = 1; i < xs.size(); i++) {
    tie(r, z) = crt_sub(r, z, as[i], xs[i]);
    if (r == -1) return -1;
  }
  return r;
}
long long int imod(long long int x, long long int y) {
  long long int v = x % y;
  if ((x >= 0) == (y >= 0))
    return v;
  else
    return v == 0 ? 0 : v + y;
}
long long int divFloor(long long int x, long long int y) {
  if (x > 0) {
    if (y > 0)
      return x / y;
    else
      return (x - y - 1) / y;
  } else {
    if (y > 0)
      return (x - y + 1) / y;
    else
      return x / y;
  }
}
long long int divCeil(long long int x, long long int y) {
  if (x > 0) {
    if (y > 0)
      return (x + y - 1) / y;
    else
      return x / y;
  } else {
    if (y > 0)
      return x / y;
    else
      return (x + y + 1) / y;
  }
}
struct Fp {
  static long long int MOD;
  long long int val;
  Fp(long long int t = 0) : val(imod(t, MOD)) {}
  Fp(const Fp& t) : val(t.val) {}
  Fp& operator=(const Fp& t) {
    val = t.val;
    return *this;
  }
  Fp& operator=(long long int t) {
    val = imod(t, MOD);
    return *this;
  }
  Fp& operator=(int t) {
    val = imod(t, MOD);
    return *this;
  }
  Fp& operator+=(const Fp& t) {
    val += t.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  Fp& operator-=(const Fp& t) {
    val -= t.val;
    if (val < 0) val += MOD;
    return *this;
  }
  Fp& operator*=(const Fp& t) {
    val = (val * t.val) % MOD;
    return *this;
  }
  Fp inv() const {
    if (val == 0) {
      cerr << "inv() is called for zero." << endl;
      exit(1);
    }
    auto [g, u, v] = eGCD(val, MOD);
    return Fp(u);
  }
  Fp& operator/=(const Fp& t) { return (*this) *= t.inv(); }
  Fp operator+(const Fp& t) const { return Fp(val) += t; }
  Fp operator-(const Fp& t) const { return Fp(val) -= t; }
  Fp operator*(const Fp& t) const { return Fp(val) *= t; }
  Fp operator/(const Fp& t) const { return Fp(val) /= t; }
  Fp operator-() const { return Fp(-val); }
  bool operator==(const Fp& t) const { return val == t.val; }
  bool operator!=(const Fp& t) const { return val != t.val; }
  operator long long int() const { return val; }
};
Fp operator+(int x, const Fp& y) { return Fp(x) + y; }
Fp operator-(int x, const Fp& y) { return Fp(x) - y; }
Fp operator*(int x, const Fp& y) { return Fp(x) * y; }
Fp operator/(int x, const Fp& y) { return Fp(x) / y; }
Fp operator+(long long int x, const Fp& y) { return Fp(x) + y; }
Fp operator-(long long int x, const Fp& y) { return Fp(x) - y; }
Fp operator*(long long int x, const Fp& y) { return Fp(x) * y; }
Fp operator/(long long int x, const Fp& y) { return Fp(x) / y; }
Fp operator+(const Fp& x, int y) { return x + Fp(y); }
Fp operator-(const Fp& x, int y) { return x - Fp(y); }
Fp operator*(const Fp& x, int y) { return x * Fp(y); }
Fp operator/(const Fp& x, int y) { return x / Fp(y); }
Fp operator+(const Fp& x, long long int y) { return x + Fp(y); }
Fp operator-(const Fp& x, long long int y) { return x - Fp(y); }
Fp operator*(const Fp& x, long long int y) { return x * Fp(y); }
Fp operator/(const Fp& x, long long int y) { return x / Fp(y); }
istream& operator>>(istream& is, Fp& t) {
  long long int x;
  is >> x;
  t = x;
  return is;
}
ostream& operator<<(ostream& os, const Fp& t) {
  os << t.val;
  return os;
}
class Comb {
  int nMax;
  vector<Fp> vFact;
  vector<Fp> vInvFact;

 public:
  Comb(int nm) : nMax(nm), vFact(nm + 1), vInvFact(nm + 1) {
    vFact.at(0) = 1;
    for (int i = 1; i <= nMax; i++) vFact.at(i) = i * vFact.at(i - 1);
    vInvFact.at(nMax) = vFact.at(nMax).inv();
    for (int i = nMax; i >= 1; i--) vInvFact.at(i - 1) = i * vInvFact.at(i);
  }
  Fp fact(int n) { return vFact.at(n); }
  Fp comb(int n, int r) {
    return vFact.at(n) * vInvFact.at(r) * vInvFact.at(n - r);
  }
  Fp perm(int n, int r) { return vFact.at(n) * vInvFact.at(n - r); }
};
long long int Fp::MOD = 1e9 + 7;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T1, typename T2, typename T3>
ostream& operator<<(ostream& os, const tuple<T1, T2, T3>& t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
  return os;
}
template <typename T1, typename T2, typename T3, typename T4>
ostream& operator<<(ostream& os, const tuple<T1, T2, T3, T4>& t) {
  os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", "
     << get<3>(t) << ")";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << '[';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << ']';
  return os;
}
template <typename T, typename C>
ostream& operator<<(ostream& os, const set<T, C>& v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << '}';
  return os;
}
template <typename T, typename C>
ostream& operator<<(ostream& os, const unordered_set<T, C>& v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << '}';
  return os;
}
template <typename T, typename C>
ostream& operator<<(ostream& os, const multiset<T, C>& v) {
  os << '{';
  for (auto it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  os << '}';
  return os;
}
template <typename T1, typename T2, typename C>
ostream& operator<<(ostream& os, const map<T1, T2, C>& mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin()) os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';
  return os;
}
template <typename T1, typename T2, typename C>
ostream& operator<<(ostream& os, const unordered_map<T1, T2, C>& mp) {
  os << '[';
  for (auto it = mp.begin(); it != mp.end(); it++) {
    if (it != mp.begin()) os << ", ";
    os << it->first << ": " << it->second;
  }
  os << ']';
  return os;
}
template <typename T, typename T2>
ostream& operator<<(ostream& os, const queue<T, T2>& orig) {
  queue<T, T2> que(orig);
  bool first = true;
  os << '[';
  while (!que.empty()) {
    T x = que.front();
    que.pop();
    if (!first) os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}
template <typename T, typename T2>
ostream& operator<<(ostream& os, const deque<T, T2>& orig) {
  deque<T, T2> que(orig);
  bool first = true;
  os << '[';
  while (!que.empty()) {
    T x = que.front();
    que.pop_front();
    if (!first) os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}
template <typename T, typename T2, typename T3>
ostream& operator<<(ostream& os, const priority_queue<T, T2, T3>& orig) {
  priority_queue<T, T2, T3> pq(orig);
  bool first = true;
  os << '[';
  while (!pq.empty()) {
    T x = pq.top();
    pq.pop();
    if (!first) os << ", ";
    os << x;
    first = false;
  }
  return os << ']';
}
template <typename T>
ostream& operator<<(ostream& os, const stack<T>& st) {
  stack<T> tmp(st);
  os << '[';
  bool first = true;
  while (!tmp.empty()) {
    T& t = tmp.top();
    if (first)
      first = false;
    else
      os << ", ";
    os << t;
    tmp.pop();
  }
  os << ']';
  return os;
}
ostream& operator<<(ostream& os, int8_t x) {
  os << (int32_t)x;
  return os;
}
template <class... Args>
string dbgFormat(const char* fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}
template <class Head>
void dbgLog(bool with_nl, Head&& head) {
  cerr << head;
  if (with_nl) cerr << endl;
}
template <class Head, class... Tail>
void dbgLog(bool with_nl, Head&& head, Tail&&... tail) {
  cerr << head << " ";
  dbgLog(with_nl, forward<Tail>(tail)...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);
  long long int n, m, seed, vmax;
  cin >> n >> m >> seed >> vmax;
  auto rnd = [&]() -> long long int {
    long long int ret = seed;
    seed = (seed * 7 + 13) % 1000000007;
    return ret;
  };
  vector<long long int> a(n + 1);
  using ss_t = pair<long long int, long long int>;
  map<long long int, ss_t> ss;
  for (long long int i = 1; i <= n; i++) {
    a[i] = (rnd() % vmax) + 1;
    ss[i] = ss_t(i + 1, a[i]);
  };
  ;
  for (long long int ii = 1; ii <= m; ii++) {
    long long int op = (rnd() % 4) + 1;
    long long int l = (rnd() % n) + 1;
    long long int r = (rnd() % n) + 1;
    if (l > r) swap(l, r);
    long long int x, y;
    if (op == 3)
      x = (rnd() % (r - l + 1)) + 1;
    else
      x = (rnd() % vmax) + 1;
    if (op == 4) y = (rnd() % vmax) + 1;
    r++;
    ;
    auto divide = [&ss](long long int t) -> void {
      auto it = ss.lower_bound(t);
      if (t == it->first) return;
      it--;
      auto& [j, v] = it->second;
      ss[t] = ss_t(j, v);
      j = t;
    };
    divide(l);
    if (r < n + 1) divide(r);
    if (op == 1) {
      for (auto& [i, p] : ss) {
        if (i < l) continue;
        if (r <= i) break;
        auto& [j, v] = p;
        v += x;
      }
    } else if (op == 2) {
      for (auto it = ss.begin(); it != ss.end() && it->first != r;) {
        if (it->first < l)
          it++;
        else
          it = ss.erase(it);
      }
      ss[l] = ss_t(r, x);
    } else if (op == 3) {
      vector<pair<long long int, long long int>> vec;
      for (auto [i, p] : ss) {
        if (i < l) continue;
        if (r <= i) break;
        auto [j, v] = p;
        vec.emplace_back(v, j - i);
      }
      sort(vec.begin(), vec.end());
      for (auto [v, z] : vec) {
        if (x > z)
          x -= z;
        else {
          cout << v << "\n";
          break;
        }
      }
    } else if (op == 4) {
      Fp::MOD = y;
      Fp s = 0;
      for (auto [i, p] : ss) {
        if (i < l) continue;
        if (r <= i) break;
        auto [j, v] = p;
        s += power<Fp>(Fp(v), x) * (j - i);
      }
      cout << s << "\n";
    };
  }
  return 0;
}