#include #include #include #include #include #include #include using namespace std; //#pragma comment(linker, "/STACK:102400000000,102400000000") #define FOR(i, n, m) for(int i = n; i <= m; i++) #define REP(i, n, m) for(int i = n; i >= m; i--) #define PB push_back #define MP make_pair typedef long long ll; typedef unsigned long long ull; typedef pair PII; const int INF = 1e9+7; const int maxn = 100010; const int mod = 998244353; int b[maxn], c[maxn]; int main(){ int t; cin >> t; while(t--){ ll ans = 1; int n; cin >> n; FOR(i, 1, n) scanf("%d", &b[i]); FOR(i, 1, n) scanf("%d", &c[i]); int l = b[1], r = c[1]; if(l != r) ans = 0; else{ FOR(i, 2, n) { if(ans == 0) break; if(b[i] != l && c[i] != r) ans = 0; else if(b[i] == l && c[i] == r){ if(i > r - l + 1) ans = 0; else ans = ans * (r - l + 1 - i + 1) % mod; } else if(b[i] == l && c[i] > r) r = c[i]; else if(c[i] == r && b[i] < l) l = b[i]; else ans = 0; } } cout << ans << endl; } return 0; }