#include<bits/stdc++.h>
#include<vector>
#define ll long long
#define i128 __int128
#define ull unsigned long long
#define pii pair<int,int>
#define fir first
#define se second
#define endl '\n'
#define debug puts("AK IOI RP INF")
#define debug1 puts("AK IOI -- part 1")
#define debug2 puts("AK IOI -- part 2")
#define debug3 puts("AK IOI -- part 3")
#define debug4 puts("AK IOI -- part 4")
#define re register
#define Yes puts("Yes")
#define No puts("No")
#define YES puts("YES")
#define NO puts("NO")
#define yes puts("yes")
#define no puts("no")
#define error puts("error")
#define IMPOSSIBLE puts("IMPOSSIBLE")
#define Impossible puts("Impossible")
#define fy puts("-1")
#define pll pair<long long, long long>
#define AC exit(0)
#define pb push_back
using namespace std;
namespace gsczl71_function {
inline int Min(int a, int b) {
return a < b ? a : b;
}
inline int Max(int a, int b) {
return a > b ? a : b;
}
inline void Swap(int& a, int& b) {
a ^= b, b ^= a, a ^= b;
}
inline void tomin(int& a, int b) {
if (a > b)a = b;
}
inline void tomax(int& a, int b) {
if (a < b)a = b;
}
inline int Abs(int x) {
return (x ^ (x >> 31)) - (x >> 31);
}
inline ll gcd(ll a, ll b) {
int i = __builtin_ctz(a), j = __builtin_ctz(b), x = i > j ? j : i, y;
b >>= j;
while (a) {
a >>= i;
y = b - a;
i = __builtin_ctz(y);
b = a < b ? a : b;
a = y < 0 ? -y : y;
}
return b << x;
}
inline ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
inline ll jc(ll x, ll p) {
ll res = 1;
if (p)for (ll i(1); i <= x; ++i)res = (res * i) % p;
else for (ll i(1); i <= x; ++i)res = (res * i);
return res;
}
inline ll Mod(ll x, ll y) {
return x - y * (x / y);
}
inline ll ksm(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res *= a;
a *= a, b >>= 1;
}
return res;
}
inline ll ksm(ll a, ll b, ll p) {
ll res = 1;
while (b) {
if (b & 1) res = (res * a) % p;
a = (a * a) % p, b >>= 1;
}
return res;
}
}
namespace gsczl71_fastIO {
template <typename T = int>
inline T read() {
T s = 0, w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-')w = -1;
c = getchar();
}
while (isdigit(c))s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
return s * w;
} template <typename T>
inline void read(T &s) {
s = 0;
int w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-')w = -1;
c = getchar();
}
while (isdigit(c))s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
s *= w;
} template <typename T, typename... Args> inline void read(T &x, Args &...args) {
read(x), read(args...);
}
template <typename T>
inline void write(T x, char ch) {
if (x < 0)x = -x, putchar('-');
static char stk[200];
int top = 0;
do {
stk[top++] = x % 10 + '0', x /= 10;
} while (x);
while (top)putchar(stk[--top]);
putchar(ch);
return;
}
}
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f, N = 5e5 + 5, M = 2e5 + 5;
const ll linf = 0x3f3f3f3f3f3f3f3f;
int fa[N][35];
ll res = 0;
#define ui unsigned int
ui s;
inline ui get(ui x) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return s = x;
}
ui ans[N];
int gen, dep[N];
vector<int> mp[N];
void dfs(int x, int fa) {
for (auto it : mp[x]) {
if (it == fa)continue;
dep[it] = dep[x] + 1;
dfs(it, x);
}
return ;
}
void solve() {
int n, q;
cin >> n >> q >> s;
for (int i = 1; i <= n; i++) {
cin >> fa[i][0];
if (fa[i][0] == 0) gen = i;
else mp[fa[i][0]].push_back(i);
}
dep[gen] = 1;
dfs(gen, 0);
for (int i = 1; i <= 31; i++) {
for (int j = 1; j <= n; j++) {
fa[j][i] = fa[fa[j][i - 1]][i - 1];
}
}
for (int i = 1; i <= q; i++) {
int x, k;
x = ((get(s)^ans[i - 1]) % n) + 1;
k = (get(s) ^ans[i - 1]) % dep[x];
for (int j = 0; j <= 31; j++) {
if ((1 << j) & k) x = fa[x][j];
}
ans[i] = x;
res ^= 1ll * i * ans[i];
}
cout << res;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
while (T--) solve();
return 0;
}