#include<bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-f;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
char str[5000010];
int n,fa[5000010],ans,lst[5000010],cnt=0,l[5000010],sum[5000010];
vector<int>g[5000010];
void init()
{
ans=0;
n=read();
scanf("%s",str+1);
for(int i=2;i<=n;i++)
{
fa[i]=read();
g[fa[i]].push_back(i);
}
}
void dfs(int node)
{
//cout<<node<<" Orz\n";
int tmp;
if(str[node]==')'&&cnt!=0)
{
tmp=l[cnt];
lst[node]=lst[fa[tmp]]+1;
cnt--;
}
else if(str[node]=='(')
{
cnt++;
l[cnt]=node;
}
sum[node]=sum[fa[node]]+lst[node];
for(int i=0;i<g[node].size();i++)
dfs(g[node][i]);
if(tmp!=0)
{
cnt++;
l[cnt]=tmp;
}
else if(cnt>0)
cnt--;
//cout<<"Hello,World!\n";
}
main()
{
init();
dfs(1);
//cout<<"Orz Orz Orz\n";
for(int i=1;i<=n;i++)
ans^=(long long)i*sum[i];
cout<<ans;
return 0;
}
rt