#include #include #include #include #include #define M 100005 #define Mod 998244353 int B[M],C[M],T,n; int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=1;i<=n;++i)scanf("%d",&B[i]); for(int i=1;i<=n;++i)scanf("%d",&C[i]); int ans=1,tmp=0,mx=C[1],mi=B[1]; if(B[1]!=C[1])ans=0; for(int i=2;i<=n;++i){ if(C[i]mi)ans=0; else if(C[i]!=mx&&B[i]!=mi)ans=0; else if(C[i]>mx&&B[i]==mi){tmp+=C[i]-mx-1;tmp%=Mod;mx=C[i];} else if(B[i]