#include "circuit.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,on;
vector<ll>p;
vector<ll>a;
vector<vector<ll>>inputs;
void init(ll N, ll M, vector<ll> P, vector<ll> A) {
n=N;
m=M;
vector<ll>in;
//in.push_back(0);
for(int i=0;i<n;i++){
inputs.push_back(in);
a.push_back(0);
}
for(int i=0;i<int(P.size());i++){
p.push_back(P[i]);
if(p[i]!=-1){
inputs[p[i]].push_back(i);
}
}
for(int i=0;i<int(A.size());i++){
a.push_back(A[i]);
if(A[i]==1){
on+=1;
}
}
}
ll sourceOn(ll n,ll num,vector<ll>source){
ll on=0;
/*
//print parameters
cout<<n<<"=n"<<endl;
cout<<num<<"=num"<<endl;
for(int i=0;i<int(source.size());i++){
cout<<source[i]<<" ";
}
cout<<"=source"<<endl;
*/
for(int i=0;i<int(source.size());i++){
if(a[source[i]]==1){
on+=1;
}
}
if(on!=0 and (num-1)>=0){
a[num]=1;
}else{
a[num]=0;
}
return on;
}
ll num;
ll count_ways(ll L, ll R) {
ll ans=0;
for(int i=L;i<R+1;i++){
if(a[i]==0){
a[i]=1;
}else{
a[i]=0;
}
}
/*
//print state
cout<<"[";
for(int i=0;i<int(a.size());i++){
cout<<a[i]<<",";
}
cout<<"states]"<<endl;
//print inputs
cout<<"[";
for(int i=0;i<int(inputs.size());i++){
cout<<"[";
for(int j=0;j<int(inputs[i].size());j++){
cout<<inputs[i][j]<<",";
}
cout<<"ind],";
}
cout<<"inputs]"<<endl;
*/
for(int i=n-1;i>=0;i--){
num=sourceOn(n,i,inputs[i]);
if(num>0 and ans!=0){
ans*=num;
}else if(ans==0 and num>0){
ans=num;
}
}
return ans;
}