Link to this code: https://cses.fi/paste/4c3d5a3aa4cbe9a0e41ed0/
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;

    long long x, a, b, c;
    cin >> x >> a >> b >> c;

    
    // x %= c;
    // a %= c;
    // b %= c;

    deque<pair<long long,int>> dq;
    long long ans = 0;  

    for (int i = 0; i < n; i++) 
    {
        while (!dq.empty() && dq.front().second <= i-k) 
        {
            dq.pop_front();
        }
        if (i > 0)
            x = ((a * x)%c + b) % c;
        while(!dq.empty() && dq.back().first > x)
            dq.pop_back();
        dq.push_back({x,i});

        if (i>=k-1)
            ans ^= dq.front().first;
    }

    cout << ans << "\n";
    return 0;
}