#include #include #include using namespace std; #define P 998244353 #define ll long long #define N 200005 int T,n,m,k,l,r,t; int a[N],b[N]; ll s,ans; int main(){ scanf("%d",&T); while(T--){ ans=1;s=0; scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); for (int i=1;i<=n;i++)scanf("%d",&b[i]); t=0; for (int i=2;i<=n;i++)if(a[i]!=a[i-1]&&b[i]!=b[i-1])t=1; if(a[1]!=b[1])t=1; if(t){puts("0");continue;} l=r=a[1]; for (int i=2;i<=n;i++){ if(a[i]>l||b[i]r){s+=b[i]-r-1;r=b[i];} else{ ans=ans*s%P; s--; } } if(!t)printf("%I64d\n",ans); } }