Problem:
Score : 600 points

Problem Statement

Given are an integer N and arrays S, T, U, and V, each of length N.
Construct an N×N matrix a that satisfy the following conditions:

a_{i,j} is an integer.

0 \leq a_{i,j} \lt 2^{64}.

If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.

If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.

If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.

If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.

However, there may be cases where no matrix satisfies the conditions.

Constraints

All values in input are integers.

1 \leq N \leq 500

0 \leq S_{i} \leq 1

0 \leq T_{i} \leq 1

0 \leq U_{i} \lt 2^{64}

0 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:

N
S_{1} S_{2} ... S_{N}
T_{1} T_{2} ... T_{N}
U_{1} U_{2} ... U_{N}
V_{1} V_{2} ... V_{N}

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Sample Input 1

2
0 1
1 0
1 1
1 0

Sample Output 1

1 1
1 0

In Sample Input 1, we need to find a matrix such that:

the bitwise AND of the elements in the 1-st row is 1;

the bitwise OR of the elements in the 2-nd row is 1;

the bitwise OR of the elements in the 1-st column is 1;

the bitwise AND of the elements in the 2-nd column is 0.

Sample Input 2

2
1 1
1 0
15 15
15 11

Sample Output 2

15 11
15 11

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

//#include<bits\stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 1000000000
#define ldb long double
#define pb push_back
#define get(x) x=read()
#define gt(x) scanf("%d",&x)
#define gi(x) scanf("%lf",&x)
#define put(x) printf("%d\n",x)
#define putl(x) printf("%lld\n",x)
#define gc(a) scanf("%s",a+1)
#define rep(p,n,i) for(RE ll i=p;i<=n;++i)
#define go(x) for(ll i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define fep(n,p,i) for(RE ll i=n;i>=p;--i)
#define pii pair<ll,ll>
#define mk make_pair
#define RE register
#define P 1000000007
#define mod 998244353
#define S second 
#define F first
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define ull unsigned long long
#define ui unsigned
#define zz p<<1
#define yy p<<1|1
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline ll read()
{
    register ll x=0,f=1;register char ch=getc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getc();}
    return x*f;
}
const ll MAXN=252;
int n,cc;
int s[MAXN],t[MAXN];
int v[MAXN],u[MAXN];
int b[MAXN][MAXN];
int r[MAXN][2],c[MAXN][2];
ull a[MAXN][MAXN];
inline int solve()
{
	rep(0,63,k)
	{
		memset(b,-1,sizeof(b));
		memset(c,0,sizeof(c));
		memset(r,0,sizeof(r));
		rep(1,n,i)
		{
			int x=u[i]&1;
			if(s[i]==x)continue;
			rep(1,n,j)if(b[i][j]!=-1&&b[i][j]!=x)return 0;
			else b[i][j]=x;
		}
		rep(1,n,i)
		{
			int x=v[i]&1;
			if(t[i]==x)continue;
			rep(1,n,j)if(b[j][i]!=-1&&b[j][i]!=x)return 0;
			else b[j][i]=x;
		}
		rep(1,n,i)rep(1,n,j)
		{
			if((u[i]&1)==(v[j]&1))b[i][j]=(u[i]&1);
			if(b[i][j]==-1)b[i][j]=0;
			++r[i][b[i][j]];
			++c[j][b[i][j]];
		}
		rep(1,n,i)
			if(s[i]&&(u[i]&1))
			{
				if(r[i][1])continue;
				rep(1,n,j)
				{
					if(!t[j]&&!(v[j]&1)&&c[j][0]>1)
					{
						b[i][j]=1;
						--c[j][0];--r[i][0];
						++c[j][1];++r[i][1];
						break;
					}
				}
			}
		rep(1,n,i)
		{
			if(t[i]&&(v[i]&1))
			{
				if(c[i][1])continue;
				rep(1,n,j)
				{
					if(!s[j]&&!(u[j]&1)&&r[j][0]>1)
					{
						b[j][i]=1;
						--r[j][0];--c[i][0];
						++r[j][1];++c[i][1];
						break;
					}
				}
			}
		}
		rep(1,n,i)
		{
			if((u[i]&1)&&!s[i]&&r[i][1]!=n)return 0;
			if(!(u[i]&1)&&s[i]&&r[i][0]!=n)return 0;
			if(!(v[i]&1)&&t[i]&&c[i][0]!=n)return 0;
			if((v[i]&1)&&!t[i]&&c[i][1]!=n)return 0;
			if((u[i]&1)==s[i]&&!s[i]&&!r[i][0])return 0;
			if((u[i]&1)==s[i]&&s[i]&&!r[i][1])return 0;
			if((v[i]&1)==t[i]&&t[i]&&!c[i][1])return 0;
			if((v[i]&1)==t[i]&&!t[i]&&!r[i][0])return 0;
		}
		rep(1,n,i)rep(1,n,j)if(b[i][j])a[i][j]=a[i][j]|(1ull<<k);
		rep(1,n,i)u[i]>>=1,v[i]>>=1;
	}
	return 1;
}
signed main()
{
	//freopen("1.in","r",stdin);
	get(n);
	rep(1,n,i)get(s[i]);
	rep(1,n,i)get(t[i]);
	rep(1,n,i)get(u[i]);
	rep(1,n,i)get(v[i]);
	if(!solve())puts("-1");
	else rep(1,n,i)rep(1,n,j)printf("%llu%c",a[i][j],j!=n?' ':'\n');
	return 0;
}