#include using namespace std; #define ll long long #define mod 998244353LL const ll all=(1ll<<60)%mod; ll c[100010],h[100010],suf[100010],num[100010]; int from[100010],to[100010],n,tt; bool bo[100010],vis[100010]; inline ll rd() { ll x=0;char ch=getchar(); for (;ch<'0'||ch>'9';ch=getchar()); for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; return x; } inline ll pls(const ll &x,const ll &y) { return (x+y>=1,x=x*x%mod) if (y&1) res=res*x%mod;return res; } inline int find(int x) { return (x==from[x])?x:from[x]=find(from[x]); } int main() { n=rd(); for (int i=1;i<=n;i++) to[i]=rd(); for (int i=1;i<=n;i++) c[i]=rd(); for (int i=1;i<=n;i++) from[i]=i; for (int i=1;i<=n;i++) { int f1=find(i),f2=find(to[i]); if (f1==f2) bo[i]=true; from[f1]=f2; } ll ans=1; for (int hhh=1;hhh<=n;hhh++) if (bo[hhh]) { tt=0; for (int j=hhh;!vis[j];j=to[j]) h[++tt]=c[j],vis[j]=true; ll now=0,res=0; for (int i=1;i<=tt;i++) now^=h[i]; if (now==0) res=1; for (int i=59;~i;i--) { int cnt=0; for (int j=1;j<=tt;j++) if ((h[j]>>i)&1) cnt++; if (!cnt) continue; ll cur=(1LL<>i)&1)) { h0=h0*num[j]%mod; h1=h1*num[j]%mod; continue; } now++; if ((cnt-now)&1) res=pls(res,suf[j+1]*h1%mod); else res=pls(res,suf[j+1]*h0%mod); ll hh0=h0,hh1=h1; h0=h0*hh%mod;h1=h1*hh%mod; h0=pls(h0,hh1*num[j]%mod); h1=pls(h1,hh0*num[j]%mod); } if (cnt&1) break; } ans=ans*res%mod*all%mod; } for (int i=1;i<=n;i++) if (!vis[i]) ans=ans*((c[i]+1)%mod)%mod; printf("%lld\n",ans); return 0; }