#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
char a[500005];
int fa[500005], k[500005];
int last[500005];
vector<int> child[500005];
void dfs(int no)
{
if (no != 1)
{
k[no] = k[fa[no]];
if (a[no] == ')')
{
int rs = 1, tmp = last[fa[no]];
while (tmp != 0)
{
if (a[tmp] == '(')
--rs;
else
++rs;
if (rs == 0)
++k[no], tmp = fa[tmp];
else if (rs == 1)
tmp = last[fa[tmp]];
else if (rs < 0)
{
last[no] = tmp;
break;
}
else
tmp = fa[tmp];
}
}
else
last[no] = no;
}
else if (a[1] == '(')
last[1] = 1;
for (int i : child[no])
dfs(i);
}
signed main()
{
scanf("%lld\n", &n);
for (int i = 1; i <= n; ++i)
a[i] = getchar();
for (int i = 2; i <= n; ++i)
{
scanf("%lld", &fa[i]);
child[fa[i]].emplace_back(i);
}
dfs(1);
int anss = k[1];
for (int i = 2; i <= n; ++i)
anss = anss ^ (i * k[i]);
printf("%lld\n", anss);
return 0;
}