#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int N = 500010;
int n, dp[N], k[N];
vector<int> g[N];
char c[N];
stack<pair<int, char> > s;
static void dfs(int x, int fa, int dep)
{
int t = 0;
if (s.empty()) s.push(make_pair(dep, c[x]));
else if (c[x] == '(') s.push(make_pair(dep, c[x]));
else if (s.top().second == ')') s.push(make_pair(dep, c[x]));
else t = s.top().first, dp[dep] = dp[t - 1] + 1, s.pop();
k[x] = k[fa] + dp[dep];
for (int y : g[x]) if (y != fa) dfs(y, x, dep + 1);
if (!t) s.pop();
else s.push(make_pair(t, '('));
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> c[i];
for (int i = 2; i <= n; i++)
{
int f; cin >> f; g[f].push_back(i);
}
dfs(1, 0, 1);
int ans = 0;
for (int i = 1; i <= n; i++) ans ^= i * k[i];
printf("%d", ans);
return 0;
}