#include <bits/stdc++.h>
using namespace std;
const long long MAX = 5 * pow(10, 5);
string a;
static long long cnnt, s[MAX + 5], lst[MAX + 5], father[MAX + 5], sum[MAX + 5], head[MAX + 5], nxt[MAX + 5], to[MAX + 5], p[MAX + 5], cnt;
long long n;
void add(long long u, long long v) {
nxt[++cnnt] = head[u];
to[cnnt] = v;
head[u] = cnnt;
}
void fop() {
freopen("brackets.in", "r", stdin);
freopen("brackets.out", "w", stdout);
}
void dfs(long long x) {
if (a[x] == ')') {
if (cnt != 0) {
p[x] = p[father[lst[cnt]]] + 1;
--cnt;
}
} else if (a[x] == '(') lst[++cnt] = x;
sum[x] = sum[father[x]] + p[x];
for (long long i = head[x]; i != 0; i = nxt[i]) {
dfs(to[i]);
}
lst[++cnt] = lst[cnt];
}
signed main() {
fop();
scanf("%lld", &n);
cin >> a;
a = ' ' + a;
for (long long i = 2; i <= n; i = -~i) {
long long x;
scanf("%lld", &x);
add(x, i);
father[i] = x;
}
dfs(1);
long long ans = 0;
for (long long i = 1; i <= n; i = -~i) {
ans ^= (long long)i * sum[i];
}
cout << ans;
}