#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
ll dp[400][400][135],c[405][405],f[405],r[405],rui[405][405][135];
int n,a[405],b[405],pos[405],pos2[405],t[405][405];
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
int main(){
	cin>>n;
	repn(i,n)cin>>a[i],pos[a[i]]=i;
	repn(i,n)cin>>b[i],pos2[b[i]] = i;\
	rep(i,405)rep(j,i+1){
		if(j==0||j==i) c[i][j]=1;
		else c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
	}
	f[0]=r[0]=1;
	repn(i,400) f[i]=f[i-1]*1LL*i%mod,r[i]=modpow(f[i],mod-2);
	rep(i,n+1)rep(j,n+1){
		repn(k,n){
			if(pos[k]<=i||pos2[k]<=j) t[i][j]++;
		}
	}
	dp[0][0][0] = 1;
	repn(k,n/3){
		repn(x,n){
			repn(y,n){
				rui[x][y][k]=rui[x-1][y][k]+rui[x][y-1][k]-rui[x-1][y-1][k];
				if(x<pos[b[y]] && y<pos2[a[x]] && 2*k<=t[x][y] && t[x][y]<=3*k-1){
					dp[x][y][k]=rui[x-1][y-1][k-1];
					if(k==1)dp[x][y][k]++;
					dp[x][y][k]=dp[x][y][k]%mod*r[3*k-3-t[x-1][y-1]]%mod;
					rui[x][y][k] += dp[x][y][k]*f[3*k-t[x][y]]%mod;
				}
				rui[x][y][k]%=mod;
			}
		}
	}
	ll ans=0;
	rep(i,n+1)rep(j,n+1)ans = (ans+dp[i][j][n/3]*f[n-t[i][j]]%mod)%mod;
	rep(i,n)if(i%3!=0)ans=ans*1LL*i%mod;
	cout<<(ans%mod+mod)%mod<<endl;
}
