-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1534C.cpp
50 lines (42 loc) · 1.14 KB
/
1534C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
#define ll long long int
#define show(x) cout << #x << " : " << x << endl
#define mod 1000000007
using namespace std;
ll bigmod(ll a, ll p)
{
if (p==0)return 1;
ll temp = bigmod(a,p/2);
temp = (temp*temp)%mod;
if (p%2==1)temp = (temp*a)%mod;
return temp;
}
int main()
{
int tc;
scanf("%d",&tc);
while(tc--){
ll n;
scanf("%lld",&n);
ll A[n+5] , id[n+5] , vis[n+5] , B[n+5] , cnt = 0;
memset(vis,0,sizeof vis);
for (int i=1 ; i<=n ; i++){
scanf("%lld",&A[i]);
id[A[i]] = i;
}
for (int i=1 ; i<=n ; i++)scanf("%lld",&B[i]);
for (int i=1 ; i<=n ; i++){
if (vis[i]==0){
cnt++;
ll a = i;
while(true){
vis[a] = 1;
a = id[B[a]];
if (vis[a]==1)break;
}
}
}
printf("%lld\n",bigmod(2,cnt));
}
return 0;
}