#include #include using namespace std; typedef long long ll; const int maxn=100010; const int mod=998244353; int B[maxn],C[maxn]; int main(){ int t; cin>>t; while(t--){ int n; cin>>n; for(int i=1;i<=n;i++){ scanf("%d",&B[i]); } for(int i=1;i<=n;i++){ scanf("%d",&C[i]); } ll ans=1,Min,Max; if(B[1]!=C[1]){ cout<<0<Min||C[i]Max)){ ans=0;break; }else{ if(B[i]==Min&&C[i]==Max){ ans=ans*(Max-Min+1-i+1)%mod; }else{ Min=B[i];Max=C[i]; } } } cout<