#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N = (int)1e5 + 12, MOD = 1000002022;
int n, m, f, a[N];
vector<ll> g[N], dp[N], tdp[N];
void add(int &x, int y) {
x += y;
x %= MOD;
}
ll pd[N][2];
void dfs(int v) {
if(v >= n) {
pd[v][a[v]] = 1;
pd[v][1 - a[v]] = 0;
return;
}
for(int to : g[v]) {
dfs(to);
}
pd[v][0] = ((pd[g[v][0]][0] * pd[g[v][1]][0]) % MOD * 2ll % MOD + pd[x][0] * pd[y][1] % MOD + pd[x][1] * pd[y][0] % MOD) % MOD;
int x = g[v][0], y = g[v][1];
pd[v][1] = (pd[x][0] * pd[y][1] % MOD + pd[x][1] * pd[y][0] % MOD + pd[x][1] * pd[y][1] % MOD * 2ll % MOD) % MOD;
}
void init(int32_t NN, int32_t MM, std::vector<int32_t> P, std::vector<int32_t> A) {
n = NN;
m = MM;
f = n + m;
for(int i = n; i < f; i++) {
a[i] = A[i - n];
}
for(int i = 1; i < f; i++) {
g[P[i]].push_back(i);
}
for(int i = 0; i < f; i++) {
dp[i].resize((int)g[i].size() + 1);
tdp[i].resize((int)g[i].size() + 1);
}
}
int32_t count_ways(int32_t L, int32_t R) {
if(n > 1000) return 0;
for(int i = L; i <= R; i++) {
a[i] ^= 1;
}
dfs(0);
assert(pd[0][1] >= 0);
return pd[0][1];
}