Link to this code: https://cses.fi/paste/a0140c746968381fe1ba9b/
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#define ll long long
#define vi vector<int>
#define pii pair<int,int>

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// int mod = 998244353;
int mod = 1e9 + 7;
int MAX = 1e6+10;
std::vector<ll>fact(MAX),inv_fact(MAX) ;

ll modInv(ll a) {
    ll result = 1, exp = mod - 2;
    while (exp > 0) {
        if (exp & 1) 
            result = (result * a) % mod;
        a = (a * a) % mod;
        exp >>= 1;
    }
    return result;
}
void factorials(){
    fact[0] = 1;
    for (int i = 1; i < MAX; i++) 
        fact[i] = fact[i - 1] * i % mod;

    inv_fact[MAX - 1] = modInv(fact[MAX - 1]);
    for (int i = MAX - 2; i >= 0; i--) 
        inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod;
}
ll mod_pow(int a,int b, int mod){
    if(b == 0)return 1;
    if(b%2){
        return a*mod_pow(a,b-1,mod) % mod;
    }
    ll res = mod_pow(a,b/2,mod);
    return res *res % mod;
}
ll ncr(int n, int r) {
    if (r < 0 || n < r) 
        return 0;

    ll numerator = fact[n];
    ll denominator = (inv_fact[r] * inv_fact[n - r]) % mod;

    return (numerator * denominator) % mod;
}


void solve() {
    ll n ,k , x, a, b , c;
    cin >> n >> k >> x >> a >> b >> c;
    vector<ll> vec(n + 1);
    stack<pii>st1, st2;
    st1.push({x,x});
    ll ans = x;
    ll curr_or = 0LL;
    vec[0] = x;
    for(int i = 1;i<n;i++){
        vec[i] = (a * vec[i - 1] + b) % c;
        if(k == 1){ans = ans ^ vec[i];continue;}
        // cout << vec[i] <<" ";
        if(i<k){
            st1.push({vec[i],(st1.top().second | vec[i])});
            curr_or = st1.top().second;
            ans = curr_or;
        }
        else{
            if(st2.empty()){
                st2.push({st1.top().first, st1.top().first});
                st1.pop();
                while(!st1.empty()){
                    st2.push({st1.top().first, (st1.top().first | st2.top().second) });
                    st1.pop();
                }
            }
            st2.pop();
            if(!st1.empty())
            st1.push({vec[i], (vec[i] | st1.top().second)});
            else st1.push({vec[i],vec[i]});
            
            curr_or = st1.top().second;
            if(!st2.empty())curr_or = curr_or | st2.top().second;
            // cout <<st1.top().first <<" " << st2.top().first << " " <<curr_or<<" "<<endl;
            
            ans = ans ^ curr_or;
        }
        
    }
    // cout << endl;
        cout << ans << endl;
}

int main() {
    // your code goes here
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }

}