const modp=998244353; var t,l,n,i,j:longint; ans:int64; up,down,max,min:array[0..100000] of longint; flag:array[1..100000] of boolean; procedure tryit; var i:longint; begin fillchar(flag,sizeof(flag),true); ans:=1; if (up[1]<>down[1]) then begin writeln('0'); exit; end; max[1]:=up[1]; min[1]:=up[1]; for i:=2 to n do begin if ((up[i]<>up[i-1]) and (down[i]<>down[i-1]) and (up[i]<>down[i])) or (down[i]>down[i-1]) or (up[i]up[i-1]) then begin max[i]:=up[i]; min[i]:=up[i]; end else if (down[i]<>down[i-1]) then begin max[i]:=down[i]; min[i]:=down[i]; end else begin max[i]:=up[i]; min[i]:=down[i]; flag[i]:=false; end; if not(flag[i]) then ans:=int64(ans*(max[i]-min[i]+2-i)); if (max[i]-min[i]+2-i<0) and not(flag[i]) then begin writeln('0'); exit; end else ans:=ans mod modp; end; writeln(ans); end; begin readln(t); for l:=1 to t do begin fillchar(up,sizeof(up),0); fillchar(down,sizeof(down),0); fillchar(max,sizeof(max),0); fillchar(min,sizeof(min),0); readln(n); for i:=1 to n do read(down[i]); readln; for i:=1 to n do read(up[i]); readln; tryit; end; end.