#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>
#include <algorithm>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <ctime>
#include <chrono>


/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
*/


#ifdef LOCAL
    #define dbg(x) cout << #x << " : " << x << endl;
#else
    #define dbg(x)
#endif

#define int long long
#define pb push_back
#define ppb pop_back()
#define mp make_pair
#define fi(a, b) for (int i = a; i < b; i++)
#define fj(a, b) for (int j = a; j < b; j++)
#define fk(a, b) for (int k = a; k < b; k++)
#define fi1(a, b) for (int i = a - 1; i >= b; i--)
#define fj1(a, b) for (int j = a - 1; j >= b; j--)
#define fk1(a, b) for (int k = a - 1; k >= b; k--)
#define fx(x, a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define left leftkekmda
#define right rightkekmda

using namespace std;

template<typename T1, typename T2>inline void mine(T1 &x, const T2 &y) { if (y < x) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, const T2 &y) { if (x < y) x = y; }

template<typename T> ostream& operator << (ostream &os, const vector<T> &b) {
    for (auto &k : b) os << k << ' ';
    return os;
}
template<typename T> istream& operator >> (istream &is, vector<T> &b) {
    for (auto &k : b) is >> k;
    return is;
}


typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 5e5;

int a[2][N];
int pos[2][N];
int used[2][N];

const int MOD = 1e9 + 7;

void solve() {
    int n;
    cin >> n;
    fi(0, 2) {
        fj(0, n) {
            cin >> a[i][j];
            pos[i][a[i][j]] = j;
            used[i][j] = 0;
        }
    }
    int cur = 0;
    fi(0, n) {
        assert(used[0][i] == used[1][i]);
        if (used[0][i]) continue;
        used[0][i] = 1;
        ++cur;
        vpii q;
        q.push_back({0, i});
        while (q.size()) {
            auto[x, v] = q.back();
            q.ppb;
            if (!used[x ^ 1][v]) {
                used[x ^ 1][v] = 1;
                q.pb({x ^ 1, v});
            }
            if (!used[x ^ 1][pos[x ^ 1][a[x][v]]]) {
                used[x ^ 1][pos[x ^ 1][a[x][v]]] = 1;
                q.pb({x ^ 1, pos[x ^ 1][a[x][v]]});
            }
        }
    }
    int ans = 1;
    while (cur--) {
        ans = 2 * ans % MOD;
    }
    cout << ans << '\n';
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int t = 1;
    cin >> t;
    while (t--) solve();
}
