#include <bits/stdc++.h>
#define int long long
#define all(a) a.begin(),a.end()
#define int long long 
#define md ((int)1e9+7)
using namespace std;

namespace fast_dsu {
    const int dsu_MAXN = 100005;
    int dsu_par[dsu_MAXN], dsu_sz[dsu_MAXN];

    void init(int n) {
        for (int i = 0; i < n; i++) {
            dsu_par[i] = i;
        }
        
        memset(dsu_sz, -1, sizeof(dsu_sz));
    }

    int find(int v) {
         while (v != dsu_par[v]) dsu_par[v]=dsu_par[dsu_par[v]],v = dsu_par[v];
       // while ((v = dsu_par[v]) != dsu_par[v]);
        return v;
    }

    bool connected(int v, int u) {
        return find(v) == find(u);
    }

    int merge(int v, int u) {
         v = find(v);
         u = find(u);
        
        if (dsu_sz[v] > dsu_sz[u]) {
            int t = v;
            v = u;
            u = t;
        }
        
        dsu_par[u] = v;
        dsu_sz[v] += dsu_sz[u];
    }
}

using namespace fast_dsu;
void solve(){
    int n ;
    cin>>n ; 
    vector<int> a(n) ; 
    vector<int> b(n) ; 
    for(int&x:a) cin>>x ; 
    for(int& x:b) cin>>x ; 
    map<int,vector<int>> lol ; 
    vector<int> adj[n] ; 
    for(int i=0;i<n;i++){
        lol[a[i]].push_back(i) ; 
        lol[b[i]].push_back(i); 
    }
    for(auto x : lol){
        vector<int> f = x.second; 
   //     for(int x :f) cout << x << " "; 
     //       cout << endl ; 
        adj[f[0]].push_back(f[1]); 
        adj[f[1]].push_back(f[0]); 
    }
    int ans = 1 ; 
    vector<int> vis(n) ; 
    function<void(int)> dfs = [&](int a ){
        vis[a]=1 ; 
        for(int x :adj[a]){
            if(vis[x]) continue ; 
            dfs(x) ; 
        }
    } ;     
    for(int i=0;i<n;i++) if(vis[i]==0){ans*=2,ans%=md  ;dfs(i); } ;  
    cout << ans ; 
    return ;

}
signed main(){
    #ifndef ONLINE_JUDGE 
        freopen("in.txt","r",stdin) ; 
    #endif 
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr) ;
    int n ;
    cin>>n ;
    while(n--){
        solve() ;
        cout << '\n' ;
    }
    return 0 ; 
}
