Problem: Three men, A, B and C, are eating sushi together. Initially, there are N pieces of sushi, numbered 1 through N. Here, N is a multiple of 3.

Each of the three has likes and dislikes in sushi. A's preference is represented by (a_1,\ ...,\ a_N), a permutation of integers from 1 to N. For each i (1 \leq i \leq N), A's i-th favorite sushi is Sushi a_i. Similarly, B's and C's preferences are represented by (b_1,\ ...,\ b_N) and (c_1,\ ...,\ c_N), permutations of integers from 1 to N.

The three repeats the following action until all pieces of sushi are consumed or a fight brakes out (described later):

* Each of the three A, B and C finds his most favorite piece of sushi among the remaining pieces. Let these pieces be Sushi x, y and z, respectively. If x, y and z are all different, A, B and C eats Sushi x, y and z, respectively. Otherwise, a fight brakes out.



You are given A's and B's preferences, (a_1,\ ...,\ a_N) and (b_1,\ ...,\ b_N). How many preferences of C, (c_1,\ ...,\ c_N), leads to all the pieces of sushi being consumed without a fight? Find the count modulo 10^9+7.

Constraints

* 3 \leq N \leq 399
* N is a multiple of 3.
* (a_1,\ ...,\ a_N) and (b_1,\ ...,\ b_N) are permutations of integers from 1 to N.

Input

Input is given from Standard Input in the following format:


N
a_1 ... a_N
b_1 ... b_N


Output

Print the number of the preferences of C that leads to all the pieces of sushi being consumed without a fight, modulo 10^9+7.

Examples

Input

3
1 2 3
2 3 1


Output

2


Input

3
1 2 3
1 2 3


Output

0


Input

6
1 2 3 4 5 6
2 1 4 3 6 5


Output

80


Input

6
1 2 3 4 5 6
6 5 4 3 2 1


Output

160


Input

9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6


Output

33600

Solution (in CPP):

Solution:
#pragma GCC target("avx")  // CPU 処理並列化
#pragma GCC optimize("O3")  // CPU 処理並列化
#pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<complex>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-10;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3100000;
const int UF_SIZE = 210000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
int p[410];
int q[410];
long long dp[140][410][410];
int sh[410][410];
long long dp2[410][410];
int t[410];
vector<int>hb[410];
int st[410][410];
long long P[410][410];
int main(){
	int a;scanf("%d",&a);
	for(int i=0;i<a;i++){
		scanf("%d",p+i);
		p[i]--;
	}
	for(int i=0;i<a;i++){
		scanf("%d",q+i);
		q[i]--;
	}

	for(int i=0;i<=a;i++){
		for(int j=0;j<400;j++)t[j]=0;
		int cur=0;
		for(int j=0;j<i;j++){
			t[p[j]]++;
			if(t[p[j]]==1)cur++;
		}
		for(int j=0;j<=a;j++){
			sh[i][j]=cur;
			if(j<a){
				t[q[j]]++;
				if(t[q[j]]==1)cur++;
			}
		}
	}
	for(int i=0;i<a;i++){
		for(int j=0;j<a;j++){
			st[i][j]=hb[i].size();
			if(sh[i+1][j+1]==sh[i][j]+2){
				hb[i].push_back(j);
			}
		}
	}
	init_C(1100);
	for(int i=0;i<a;i++)for(int j=0;j<a;j++){
		P[i][j]=fact[i]*finv[j]%mod;
	}
	dp[0][0][0]=1;
	for(int i=0;i<a/3;i++){
		for(int j=0;j<=a;j++){
			for(int k=0;k<=a;k++){
				if(!dp[i][j][k])continue;
				for(int l=j;l<a;l++){
					if(sh[l][k]>i*3)break;
					for(int M=st[l][k];M<hb[l].size();M++){
						int m=hb[l][M];
						if(sh[l][m]>i*3)break;
						
						int r=sh[l][m]-i*2;
						int f=sh[j][k]-i*2;
						dp[i+1][l+1][m+1]=(dp[i+1][l+1][m+1]+dp[i][j][k]*P[i-f][i-r])%mod;
						
					}
				}
			}
		}
	}
	long long ret=0;
	for(int i=0;i<=a;i++)for(int j=0;j<=a;j++){
		int rem=a-sh[i][j];
		ret+=dp[a/3][i][j]*fact[rem]%mod;
	}
	ret%=mod;
	long long ret2=0;
	dp2[0][0]=1;
	for(int i=0;i<a;i++){
		for(int j=0;j<a;j++){
			if(!dp2[i][j])continue;
			dp2[i+1][j+2]=(dp2[i+1][j+2]+dp2[i][j])%mod;
			if(j)dp2[i+1][j-1]=(dp2[i+1][j-1]+dp2[i][j]*j)%mod;	
		}
	}
	ret2=dp2[a][0];
	ret=ret*ret2%mod;
	printf("%lld\n",ret);
}