#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()

const int mod=1e9+7;
const int inf=1e9+10;
const ll INF=1e18+10;

vector<int> pfactors(int n){
    vector<int> res;
    while(n%2==0){
        res.push_back(2);
        n/=2;
    }
    for(int i=3;i<=sqrt(n);i++){
        while(n%i==0){
            res.push_back(i);
            n/=i;
        }
    }
    if(n>2) res.push_back(n);
    return res;
}

bool isPrime(ll a){
    if(a<=1) return false;
    if(a<=3) return true;
    if(a%2==0 or a%3==0) return false;
    for(ll i=5;i*i<a;i+=6)
        if(a%i==0 or a%(i+2)==0) return false;
    return true;
}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("../read.txt","r",stdin);
    // freopen("write.txt","w",stdout);
    #endif //LOCAL

    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    int t; 
    cin>>t;
    // t=1;
    while(t--){
        int n; cin>>n;
        int loop=0,fi,sec;
        unordered_map<int,int> m,m2;
        vector<int> v(n),u(n);
        vector<int> vis(n,0);
        for(auto &x:v) cin>>x;
        for(auto i=0;i<n;i++) m2[v[i]]=i;
        for(auto &x:u) cin>>x;
        for(int i=0;i<n;i++){
            m[v[i]]=u[i];

        }
        for(int i=0;i<n;i++){
            if(!vis[i]){
                loop++;
                vis[i]=1;
                fi=v[i];
                sec=u[i];
                while(sec!=fi){
                    sec=u[m2[sec]];
                    vis[m2[sec]]=1;
                }
            }
        }
        int ans=1;
        for(int i=0;i<loop/2;i++){
            ans*=2;
            ans%=mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}