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++ (Clang 10.0.0) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <string>
 
#ifndef ONLINE_JUDGE
	#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#else
	#define DEBUG(x) do {} while(0);
#endif
using namespace std;

/*
když Si=0 & Tj=0-> A[i,j] = Ui|Vj

když Si=0 & Tj=1 & Ui=0 & Vj=1 -> A[i,j] = Ui|0 
když Si=0 & Tj=1 & Ui=0 & Vj=0 -> 0
když Si=0 & Tj=1 & Ui=1 & Vj=0 -> nema reseni
když Si=0 & Tj=1 & Ui=1 & Vj=1 -> 1


když Si=1 & Tj=1 -> A[i,j] = Ui&Vj
*/
 
#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(), (x).end()
#define PER(i, N) for(int (i) = (N)-1; (i) >= 0; (i)--)
#define REP(i, N) for(int (i) = 0; (i) < (int)(N); (i)++)
#define rep(i, j, N) for(int (i) = j; (i) < (int)(N); (i)++)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
 
typedef pair<int, int> pii;
typedef vector<int> vi;
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }

template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template <typename T, typename U> std::ostream&operator<<(std::ostream&o, const pair<T,U>&p) {o << p.x << ' ' << p.y; return o;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {if(t.empty())o<<'\n';for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }
const int MOD = 1000000007;
ull mtx[555][555];
void solve(istream& cin, ostream& cout) {
	int n;
	cin >> n;
	vector<ull> S(n), T(n), U(n), V(n);
	cin >> S >> T >> U >> V;
	bool ok = true;
	REP(i, n) REP(j, n) {
		if(S[i]==1&&T[j]==1) mtx[i][j] = U[i]&V[j];
		else if(S[i]==0&&T[j]==0) mtx[i][j] = U[i]|V[j];
		else {
			if(S[i] == 0) mtx[i][j] = i == j ? (U[i]|V[j]) : U[i];
			else if(T[j]==0) mtx[i][j] = i==j? (U[i]|V[j]) : V[j];
		}
	}
	REP(i, n) {
		ull rRes = mtx[i][0];
		if(S[i] == 0)
			REP(j, n) rRes = rRes&mtx[i][j];
		else
			REP(j, n) rRes = rRes|mtx[i][j];
		if(rRes != U[i])
			ok = false;
	}
	REP(j, n) {
		ull cRes = mtx[0][j];
		if(T[j] == 0)
			REP(i, n) cRes = cRes&mtx[i][j];
		else
			REP(i, n) cRes = cRes|mtx[i][j];
		if(cRes != V[j])
			ok = false;
	}
	if(!ok) cout <<-1<<endl;
	else {
		REP(i, n) {
			REP(j, n) cout <<mtx[i][j] <<" ";
			cout <<endl;
		}
	}
}


int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	solve(cin, cout);
	/*
	{
		istringstream instr("2\n2\n14");
		solve(instr, cout);
	}
 */
	return 0;
}