#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long int int64;

const int MOD = (int) 1e9 + 7;
void sadd(int &a, int b)
{
    a += b;
    if (a >= MOD) a -= MOD;
}
int add(int a, int b)
{
    sadd(a, b);
    return a;
}
int mul(int a, int b)
{
    return (a * 1LL * b) % MOD;
}


const int N = 414;
int a[2][N];
int pos[2][N];

int cnt[N][N][N / 3];
int dp[N][N][N / 3];
int xdp[N][N][N / 3];
int fact[N];
int adp[N][N];


int main(int,  char **)
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

    fact[0] = 1;
    for (int i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);

    int n;
    scanf("%d", &n);
    for (int t = 0; t < 2; t++)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[t][i] );
            a[t][i]--;
            pos[t][a[t][i] ] = i;
        }
    }
    if (n > 400) throw;
    if (n % 3 != 0) throw;
    int ans = 0;
    int need = n / 3;
    dp[0][0][0] = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            for (int x = 0; x <= need; x++)
            {
                int curcnt = cnt[i][j][x];
                int cur = dp[i][j][x];
                if (cur == 0) continue;
             //   eprintf("i = %d, j = %d, x = %d : %d (%d)\n", i, j, x, cur, curcnt);
                if (curcnt == need)
                {
                    sadd(ans, mul(cur, fact[x] ) );
                    continue;
                }
                if (i < n && pos[1][a[0][i] ] < j)
                {
                   // if (cnt[i + 1][j][x] != 0 && cnt[i + 1][j][x] != curcnt) throw;
                    cnt[i + 1][j][x] = curcnt;
                    sadd(dp[i + 1][j][x], cur);
                    continue;
                }
                if (j < n && pos[0][a[1][j] ] < i)
                {
                   // if (cnt[i][j + 1][x] != 0 && cnt[i][j + 1][x] != curcnt) throw;
                    cnt[i][j + 1][x] = curcnt;
                    sadd(dp[i][j + 1][x], cur);
                    continue;
                }
                if (a[0][i] != a[1][j] )
                {
                   // if (cnt[i + 1][j + 1][x + 1] != 0 && cnt[i + 1][j + 1][x + 1] != curcnt + 1) throw;
                    cnt[i + 1][j + 1][x + 1] = curcnt + 1;
                    sadd(dp[i + 1][j + 1][x + 1], cur);
                }
                if (x > 0)
                {
                   // if (cnt[i + 1][j][x - 1] != 0 && cnt[i + 1][j][x - 1] != curcnt) throw;
                    cnt[i + 1][j][x - 1] = curcnt;
                    sadd(dp[i + 1][j][x - 1], mul(cur, x) );
                    if (a[0][i] != a[1][j] )
                    {
                       // if (cnt[i][j + 1][x - 1] != 0 && cnt[i][j + 1][x - 1] != curcnt) throw;
                        cnt[i][j + 1][x - 1] = curcnt;
                        sadd(xdp[i][j + 1][x - 1], mul(cur, x) );
                    }
                }
            }
            for (int x = 0; x <= need; x++)
            {
                int curcnt = cnt[i][j][x];
                int cur = xdp[i][j][x];
                if (cur == 0) continue;
                if (j < n && pos[0][a[1][j] ] < i)
                {
                   // if (cnt[i][j + 1][x] != 0 && cnt[i][j + 1][x] != curcnt) throw;
                    cnt[i][j + 1][x] = curcnt;
                    sadd(xdp[i][j + 1][x], cur);
                    continue;
                }
                if (a[0][i] != a[1][j] )
                {
                   // if (cnt[i + 1][j + 1][x + 1] != 0 && cnt[i + 1][j + 1][x + 1] != curcnt + 1) throw;
                    cnt[i + 1][j + 1][x + 1] = curcnt + 1;
                    sadd(dp[i + 1][j + 1][x + 1], cur);
                }
                if (x > 0)
                {
                    if (a[0][i] != a[1][j] )
                    {
                       // if (cnt[i][j + 1][x - 1] != 0 && cnt[i][j + 1][x - 1] != curcnt) throw;
                        cnt[i][j + 1][x - 1] = curcnt;
                        sadd(xdp[i][j + 1][x - 1], mul(cur, x) );
                    }
                }
            }
        }
    int K = 0;
    adp[0][0] = 1;
    for (int i = 0; i <= need; i++)
        for (int j = 0; j <= 2 * i; j++)
        {
            int cur = adp[i][j];
            if (cur == 0)
                continue;
            if (j == 2 * need)
            {
                sadd(K, cur);
                continue;
            }
            if (j < 2 * i)
                sadd(adp[i][j + 1], mul(cur, 2 * i - j) );
            if (i < need)
                sadd(adp[i + 1][j], cur);
        }

    ans = mul(ans, K);

    printf("%d\n", ans);
	return 0;
}



Measured memory_bytes: 268800