#include <iostream>
#include <vector>
#include <map>
#include <cstring>
using namespace std;

using ll=int64_t;
//#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(),x.end()
#define REACH cerr<<"reached line "<<__LINE__<<endl
#define DMP(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl
#define ZERO(x) memset(x,0,sizeof(x))

using pi=pair<int,int>;
using vi=vector<int>;
using ld=long double;

template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
	os<<"[";
	REP(i,(int)v.size()){
		if(i)os<<",";
		os<<v[i];
	}
	os<<"]";
	return os;
}

int read(){
	int i;
	#ifdef int
	scanf("%" SCNd64,&i);
	#else
	scanf("%d",&i);
	#endif
	return i;
}

void printSpace(){
	printf(" ");
}

void printEoln(){
	printf("\n");
}

void print(int x,int suc=1){
	#ifdef int
	printf("%" PRId64,x);
	#else
	printf("%d\n",x);
	#endif
	if(suc==1)
		printEoln();
	if(suc==2)
		printSpace();
}

string readString(){
	static char buf[3341919];
	scanf("%s",buf);
	return string(buf);
}

char* readCharArray(){
	static char buf[3341919];
	static int bufUsed=0;
	char* ret=buf+bufUsed;
	scanf("%s",ret);
	bufUsed+=strlen(ret)+1;
	return ret;
}

template<class T,class U>
void chmax(T& a,U b){
	if(a<b)
		a=b;
}

template<class T,class U>
void chmin(T& a,U b){
	if(a>b)
		a=b;
}

template<class T>
T Sq(const T& t){
	return t*t;
}

//const int inf=LLONG_MAX/3;

const int mod=1000000007;
template<class T,class U>
void add(T& a,U b){
	a+=b;
	if(a>=mod)a-=mod;
}

template<class T,class U>
void sub(T& a,U b){
	a-=b;
	if(a<b)a+=mod;
}

template<class T,class U>
void mult(T& a,U b){
	a=((ll)a*b)%mod;
}

ll modPow(ll a,ll p){
	ll s=1;
	REP(i,30){
		if((p>>i)&1)
			mult(s,a);
		mult(a,a);
	}
	return s;
}

ll modInv(ll a){
	return modPow(a,mod-2);
}

const int Nmax=410;
const int Smax=Nmax/3;
int dp[Nmax][Nmax];
bool app[Nmax][Nmax][Nmax];
int cnt[Nmax][Nmax];
int a[Nmax],b[Nmax];
int way[Nmax];
//最後に並び替えの係数を忘れない
signed main(){
	int n=read();
	REP(i,n)a[i]=read()-1;
	REP(i,n)b[i]=read()-1;
	REP(i,n)
		app[i+1][0][a[i]]=true;
	REP(i,n)
		app[0][i+1][b[i]]=true;
	REP(i,n+1)REP(j,n+1){
		if(i)REP(k,n)app[i][j][k]|=app[i-1][j][k];
		if(j)REP(k,n)app[i][j][k]|=app[i][j-1][k];
		REP(k,n)if(app[i][j][k])cnt[i][j]++;
	}
	int s=n/3,ans;
	dp[0][0]=1;
	REP(step,s+1){
		REP(i,n)REP(j,n+1){
			int k=step-(cnt[i][j]-step*2);
			if(app[i][j][a[i]]){
				add(dp[i+1][j],dp[i][j]);
			}else if(k){
				int w=dp[i][j];
				mult(w,k);
				add(dp[i+1][j],w);
			}
		}
		REP(i,n+1)REP(j,n){
			int k=step-(cnt[i][j]-step*2);
			if(app[i][j][b[j]]){
				add(dp[i][j+1],dp[i][j]);
			}else if(k){
				int w=dp[i][j];
				mult(w,k);
				add(dp[i][j+1],w);
			}
		}
		if(step<s){
			for(int i=n-1;i>=0;i--)REP(j,n){
				if(!app[i][j][a[i]]&&!app[i][j][b[j]]&&a[i]!=b[j])
					add(dp[i+1][j+1],dp[i][j]);
				dp[i][j]=0;
			}
		}else{
			ans=dp[n][n];
		}
	}
	way[0]=1;
	REP(_,s){
		for(int i=n;i>=0;i--){
			way[i+2]=way[i];
			way[i]=0;
		}
		for(int i=n;i>=1;i--){
			int w=way[i];
			mult(w,i);
			add(way[i-1],w);
		}
	}
	mult(ans,way[0]);
	cout<<ans<<endl;
}

Measured memory_bytes: 68608