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 (Python):
def numba_compile(numba_config):
    import os, sys
    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC
        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)


import numpy as np

def solve_1bit(N, rps, cps, rv, cv):
    res = np.full((N, N), 2, dtype=np.uint64)
    if N == 1:
        res[0, 0] = rv[0]
        return res
    r_all = np.array([False, False])
    for i, (ps, v) in enumerate(zip(rps, rv)):
        if ps ^ v == 1:
            res[i, :] = v
            r_all[v] = True
    c_all = np.array([False, False])
    for i, (ps, v) in enumerate(zip(cps, cv)):
        if ps ^ v == 1:
            res[:, i] = v
            c_all[v] = True
    if (r_all[0] and c_all[1]) or (r_all[1] and c_all[0]):
        return res
    if r_all[0] and c_all[0]:
        return np.where(res==2, np.uint64(1), res)
    if r_all[1] and c_all[1]:
        return np.where(res==2, np.uint64(0), res)
    if np.all(r_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = rv[y]
        return res
    if np.all(c_all):
        for y in range(N):
            for x in range(N):
                if res[y, x] == 2:
                    res[y, x] = cv[x]
        return res
    if r_all[0]:
        if (cv==0).any():
            x = (cv==0).nonzero()[0][0]
            res[:, x] = 0
            return np.where(res==2, np.uint64(1), res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 0 and res[y, 0] == 2:
                res[y, x] = 0
                x = (x+1) % N
        return np.where(res==2, np.uint64(1), res)
    if r_all[1]:
        if (cv==1).any():
            x = (cv==1).nonzero()[0][0]
            res[:, x] = 1
            return np.where(res==2, np.uint64(0), res)
        x = 0
        for y in range(N):
            v = rv[y]
            if v == 1 and res[y, 0] == 2:
                res[y, x] = 1
                x = (x+1) % N
        return np.where(res==2, np.uint64(0), res)
    if c_all[0]:
        if (rv==0).any():
            y = (rv==0).nonzero()[0][0]
            res[y, :] = 0
            return np.where(res==2, np.uint64(1), res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 0 and res[0, x] == 2:
                res[y, x] = 0
                y = (y+1) % N
        return np.where(res==2, np.uint64(1), res)
    if c_all[1]:
        if (rv==1).any():
            y = (rv==1).nonzero()[0][0]
            res[y, :] = 1
            return np.where(res==2, np.uint64(0), res)
        y = 0
        for x in range(N):
            v = cv[x]
            if v == 1 and res[0, x] == 2:
                res[y, x] = 1
                y = (y+1) % N
        return np.where(res==2, np.uint64(0), res)
    for y in range(N):
        for x in range(N):
            res[y, x] = (y^x)&1
    return res

def solve(N, row_prod_sum, col_prod_sum, row_val, col_val):
    res = np.zeros((N, N), dtype=np.uint64)
    for i in range(64):
        rps = row_prod_sum
        cps = col_prod_sum
        rv = row_val>>i&1
        cv = col_val>>i&1
        r = solve_1bit(N, rps, cps, rv, cv)
        res |= r<<i
    
    prod = res[0].copy()
    sum_ = res[0].copy()
    for y in range(1, N):
        prod &= res[y]
        sum_ |= res[y]
    
    if (prod[col_prod_sum==0] != col_val[col_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[col_prod_sum==1] != col_val[col_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    prod = res[:, 0].copy()
    sum_ = res[:, 0].copy()
    for x in range(1, N):
        prod &= res[:, x]
        sum_ |= res[:, x]
    
    if (prod[row_prod_sum==0] != row_val[row_prod_sum==0]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    if (sum_[row_prod_sum==1] != row_val[row_prod_sum==1]).any():
        return 0, np.array([[-1]], dtype=np.int64)
    return 1, res


numba_compile([
    [solve_1bit, "u8[:,:](i8,u8[:],u8[:],u8[:],u8[:])"],
])


def main():
    N = int(input())
    row_prod_sum = np.array(input().split(), dtype=np.uint64)
    col_prod_sum = np.array(input().split(), dtype=np.uint64)
    row_val = np.array(input().split(), dtype=np.uint64)
    col_val = np.array(input().split(), dtype=np.uint64)
    t, ans = solve(N, row_prod_sum, col_prod_sum, row_val, col_val)
    if t:
        for lst in ans.tolist():
            print(" ".join(map(str, lst)))
    else:
        print(-1)

main()

Solution (Python):

"""

https://atcoder.jp/contests/abc164/tasks/abc164_f

各桁ごとに考えればよい
1.論理積が0 → 1つでも0があればおｋ → 0
2.論理積が1 → 全て1
2.論理和が0 → 全て0
1.論理和が1 → 1つでも1があればおk → 1

つまり、論路積が0,論理和が1を満たすような並べ方を見つければいい
そのような行・列が2つ以上ある場合は、01で市松模様で塗る
1つしかない場合、1でない方を見て行って、塗らなきゃいけない所だけ塗る
後は1つの方を見て行って、塗る

"""

from sys import stdin
import sys

N = int(stdin.readline())
S = list(map(int,stdin.readline().split()))
T = list(map(int,stdin.readline().split()))
U = list(map(int,stdin.readline().split()))
V = list(map(int,stdin.readline().split()))

ans = [[0] * N for i in range(N)]

for dig in range(64):
    nd = 2**dig

    a = [[None] * N for i in range(N)]

    

    h = [] 
    w = [] 

    for i in range(N):
        
        if S[i] == 0 and U[i]&nd > 0:
            for j in range(N):
                if a[i][j] == 0:
                    print (-1)
                    sys.exit()
                a[i][j] = 1
        
        elif S[i] == 1 and U[i]&nd == 0:
            for j in range(N):
                if a[i][j] == 1:
                    print (-1)
                    sys.exit()
                a[i][j] = 0
        elif S[i] == 0 and U[i]&nd == 0:
            h.append((i,0))
        else:
            h.append((i,1))

    for j in range(N):

        if T[j] == 0 and V[j]&nd > 0:
            for i in range(N):
                if a[i][j] == 0:
                    print (-1)
                    sys.exit()
                a[i][j] = 1
        elif T[j] == 1 and V[j]&nd == 0:
            for i in range(N):
                if a[i][j] == 1:
                    print (-1)
                    sys.exit()
                a[i][j] = 0
        elif T[j] == 0 and V[j]&nd == 0:
            w.append((j,0))
        else:
            w.append((j,1))

    

    

    if len(h) > 1 and len(w) > 1:
        lside = 0
        now = 0
        for i,tmp1 in h:
            now = lside
            lside ^= 1
            for j,tmp2 in w:
                a[i][j] = now
                now ^= 1

    elif len(h) == 0 or len(w) == 0: 

        for i,x in h:
            for j in range(N):
                if a[i][j] == x:
                    break
            else:
                print (-1)
                sys.exit()

        for j,x in w:
            for i in range(N):
                if a[i][j] == x:
                    break
            else:
                print (-1)
                sys.exit()

    elif len(w) == 1: 

        wj = w[0][0]
        
        for i,x in h:
            for j in range(N):
                if a[i][j] == x:
                    break
            else:
                a[i][wj] = x
                
        for j,x in w:
            flag = False
            for i in range(N):
                if a[i][j] == None:
                    a[i][j] = x
                if a[i][j] == x:
                    flag = True

            if not flag:
                print (-1)
                sys.exit()

    else: 

        hi = h[0][0]

        for j,x in w:
            for i in range(N):
                if a[i][j] == x:
                    break
            else:
                a[hi][j] = x

        for i,x in h:
            flag = False
            for j in range(N):
                if a[i][j] == None:
                    a[i][j] = x
                if a[i][j] == x:
                    flag = True
            if not flag:
                print (-1)
                sys.exit()

    
    for i in range(N):
        for j in range(N):
            ans[i][j] += nd * a[i][j]

for i in ans:
    print (*i)

Solution (Python):
import numpy as np

N = int(input())

S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = np.array(list(map(int,input().split())),dtype = np.uint64)
V = np.array(list(map(int,input().split())),dtype = np.uint64)

ans = np.zeros((N,N),dtype = np.uint64)

for n in range(64):
  u = (U>>n)&1
  v = (V>>n)&1
  su = [int(S[i]*2+u[i]) for i in range(N)]
  tv = [int(T[i]*2+v[i]) for i in range(N)]
  suzerozero = []
  suoneone = []
  tvzerozero = []
  tvoneone = []
  for i in range(N):
    if su[i] == 0:
      suzerozero.append(i)
    if su[i] == 3:
      suoneone.append(i)
  for i in range(N):
    if tv[i] == 0:
      tvzerozero.append(i)
    if tv[i] == 3:
      tvoneone.append(i)
  
  b = np.zeros((N,N),dtype = np.uint64)
  
  for i in range(N):
    if su[i] == 1:
      b[i] = np.ones(N,int)
  
  for i in range(N):
    if tv[i] == 1:
      b[:,i] = np.ones(N,int)
  
  b |= np.dot(np.array([u]).T,np.ones((1,N),dtype = np.uint64)) & np.dot(np.array([v]).T,np.ones((1,N),dtype = np.uint64)).T
  
  if 1 not in tv and 3 not in tv:
    m = len(tvzerozero)
    now = 0
    for i in suoneone:
      b[i][tvzerozero[now%m]] += 1
      now += 1
  
  if 1 not in su and 3 not in su:
    m = len(suzerozero)
    now = 0
    for i in tvoneone:
      b[suzerozero[now%m]][i] += 1
      now += 1
  ans += b<<n

def end():
  print(-1)
  exit()
for i in range(N):
  if S[i] == 0:
    now = ans[i][0]
    for j in range(1,N):
      now &= ans[i][j]
  if S[i] == 1:
    now = ans[i][0]
    for j in range(1,N):
      now |= ans[i][j]
  if now != U[i]:
    end()
for i in range(N):
  if T[i] == 0:
    now = ans[0][i]
    for j in range(1,N):
      now &= ans[j][i]
  if T[i] == 1:
    now = ans[0][i]
    for j in range(1,N):
      now |= ans[j][i]
  if now != V[i]:
    end()

for item in ans:
  print(*list(item))

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

    N = int(input())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))
    U = list(map(int, input().split()))
    V = list(map(int, input().split()))

    range_N = range(N)
    ans = [[0]*N for _ in range_N]
    
    for i in range_N:
        if S[i] == 0:
            for j in range_N:
                ans[i][j] = ans[i][j] | U[i]
        if T[i] == 0:   
            for j in range_N:
                ans[j][i] = ans[j][i] | V[i]
    for i in range_N:
        for j in range_N:
            if (U[i] & V[j]):
                ans[i][j] = ans[i][j] | (U[i] & V[j])

    
    for x in range_N:
        if S[x] == 0:
            continue
        x_sum = ans[x][0]
        for y in range_N:
            x_sum = x_sum | ans[x][y]
        if x_sum == U[x]:
            continue
        up = U[x] - x_sum
        for y in range_N:
            if T[y]:
                continue
            y_mul = ans[0][y]
            for i in range_N:
                if i == x:
                    continue
                y_mul = y_mul & ans[i][y]
            up_y = (~y_mul) & up
            ans[x][y] += up_y
            up -= up_y
            if up == 0:
                break

    for y in range_N:
        if T[y] == 0:
            continue
        y_sum = ans[0][y]
        for x in range_N:
            y_sum = y_sum | ans[x][y]
        if y_sum == V[y]: 
            continue
        up = V[y] - y_sum
        for x in range_N:
            if S[x]:
                continue
            x_mul = ans[x][0]
            for j in range_N:
                if y == j:
                    continue
                x_mul = x_mul & ans[x][j]
            up_x = (~x_mul) & up
            ans[x][y] += up_x
            up -= up_x
            if up == 0:
                break

    
    for i in range_N:
        check_xs = ans[i][0]
        check_ys = ans[0][i]
        check_xm = ans[i][0]
        check_ym = ans[0][i]
        for j in range_N:
            check_xs = check_xs | ans[i][j]
            check_ys = check_ys | ans[j][i]
            check_xm = check_xm & ans[i][j]
            check_ym = check_ym & ans[j][i]
        if (S[i] and U[i] != check_xs) \
        or (T[i] and V[i] != check_ys) \
        or (S[i] == 0 and U[i] != check_xm) \
        or (T[i] == 0 and V[i] != check_ym) :
            print(-1)
            return       

    for i in range_N:
        print(*ans[i])

main()

Solution (Python):
n,*s=map(int,open(0).read().split());s,t,f,g=[s[i*n:i*n+n]for i in range(4)];r=range(n);a=[n*[0]for _ in r];e=enumerate
for b in range(64):
 y=1<<b;u=[k&y for k in f];v=[k&y for k in g];l=[0]*n;m=l[:]
 for i,k in e(u):
  for j,h in e(v):
   if(k&h)|((s[i]^1)*k)|((t[j]^1)*h):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1;break
  if(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(s[j]^1)*(n-l[j]>1):a[j][i]|=y;l[j]+=1;break
h=a[0][:];w=[i[0]for i in a]
for i,k in e(a):
 for j,l in e(k):w[i]=(l|w[i]if s[i]else w[i]&l);h[j]=(l|h[j]if t[j]else h[j]&l)
if g+f!=h+w:exit(print(-1))
for i in a:print(*i)

Solution (Python):
n,*s=map(int,open(0).read().split());s,t,f,g=[s[i*n:i*n+n]for i in range(4)];r=range(n);a=[n*[0]for _ in r];e=enumerate
for b in range(64):
 y=1<<b;u=[k&y for k in f];v=[k&y for k in g];l=[0]*n;m=l[:]
 for i,k in e(u):
  for j,h in e(v):
   if(k&h)|((s[i]^1)*k)|((t[j]^1)*h):a[i][j]|=y;l[i]+=1;m[j]+=1
 for i in r:
  if(l[i]==0)*s[i]*u[i]:
   for j in r:
    if(t[j]^1)*(n-m[j]>1):a[i][j]|=y;m[j]+=1;break
  if(m[i]==0)*t[i]*v[i]:
   for j in r:
    if(s[j]^1)*(n-l[j]>1):a[j][i]|=y;l[j]+=1;break
h=a[0][:];w=[i[0]for i in a]
for i,k in e(a):
 for j,l in e(k):w[i]=(l|w[i]if s[i]else w[i]&l);h[j]=(l|h[j]if t[j]else h[j]&l)
if(g!=h)|(f!=w):exit(print(-1))
for i in a:print(*i)

Solution (Python):
n=int(input())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(lambda x:'0'*65+bin(int(x))[2:],input().split()))
v=list(map(lambda x:'0'*65+bin(int(x))[2:],input().split()))
ans=[[0]*n for _ in range(n)]
for i in range(65): 
    ansi=[[-1]*n for _ in range(n)] 
    for r in range(n): 
        if s[r]==1 and u[r][-i-1]=='0':
            for c in range(n):
                ansi[r][c]=0
        elif s[r]==0 and u[r][-i-1]=='1':
            for c in range(n):
                ansi[r][c]=1
    for c in range(n):
        if t[c]==1 and v[c][-i-1]=='0':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=0
                elif ansi[r][c]==1:
                    print(-1)
                    exit(0)
        elif t[c]==0 and v[c][-i-1]=='1':
            for r in range(n):
                if ansi[r][c]==-1:
                    ansi[r][c]=1
                elif ansi[r][c]==0:
                    print(-1)
                    exit(0)
    for r in range(n):
        for c in range(n):
            if ansi[r][c]==-1 and u[r][-i-1]==v[c][-i-1]:
                ansi[r][c]=int(v[c][-i-1])


    for r in range(n): 
        if s[r]==1 and u[r][-i-1]=='1' and 1>max(ansi[r]): 
            ok=False
            for c in range(n):
                if ansi[r][c]<1 and t[c]==0 and v[c][-1-i]=='0' and 1>min([ansi[l][c] for l in range(n) if l!=r]): 
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
        if s[r]==0 and u[r][-i-1]=='0' and 0<min(ansi[r]): 
            ok=False
            for c in range(n):
                if ansi[r][c]==1 and t[c]==1 and v[c][-1-i]=='1' and 0<max([ansi[l][c] for l in range(n) if l!=r]): 
                    ansi[r][c]=0
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
    for c in range(n): 
        if t[c]==1 and v[c][-i-1]=='1' and 1>max([ansi[l][c] for l in range(n)]): 
            ok=False
            for r in range(n):
                if ansi[r][c]<1 and s[r]==0 and u[r][-1-i]=='0' and 1>min([ansi[r][l] for l in range(n) if l!=c]): 
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
        if t[c]==0 and v[c][-i-1]=='0' and 0<min([ansi[l][c] for l in range(n)]): 
            ok=False
            for r in range(n):
                if ansi[r][c]==1 and s[c]==1 and u[c][-1-i]=='1' and 0<max([ansi[r][l] for l in range(n) if l!=c]): 
                    ansi[r][c]=1
                    ok=True
                    break
            if ok==False:
                print(-1)
                exit(0)
    for r in range(n):
        for c in range(n):
            ans[r][c]+=pow(2,i)*max(0,ansi[r][c])
for a in ans:
    print(' '.join(map(str,a)))

Solution (Python):
N = int(input())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

def invalid_exit():
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    row_has_0 = [0] * N
    col_has_0 = [0] * N
    row_has_1 = [0] * N
    col_has_1 = [0] * N
    def fill(i,j,bit):
        if bit == 1 - tmp[i][j]: invalid_exit()
        tmp[i][j] = bit
        if bit:
            row_has_1[i] = col_has_1[j] = 1
        else:
            row_has_0[i] = col_has_0[j] = 1
    blank_rows = set(range(N))
    blank_cols = set(range(N))
    for i,(s,u) in enumerate(zip(S,U)):
        x = (u>>k)&1
        if s==0 and x:
            for j in range(N):
                fill(i,j,1)
            blank_rows.remove(i)
        elif s and x==0:
            for j in range(N):
                fill(i,j,0)
            blank_rows.remove(i)
    for j,(t,v) in enumerate(zip(T,V)):
        x = (v>>k)&1
        if t==0 and x:
            for i in range(N):
                fill(i,j,1)
            blank_cols.remove(j)
        elif t and x==0:
            for i in range(N):
                fill(i,j,0)
            blank_cols.remove(j)

    if blank_rows or blank_cols:
        j = list(blank_cols)[0] if len(blank_cols)==1 else None
        for i,(s,u) in enumerate(zip(S,U)):
            x = (u>>k)&1
            if s==x:
                if s==0 and (not row_has_0[i]) and len(blank_cols) == 0: invalid_exit()
                if s==1 and (not row_has_1[i]) and len(blank_cols) == 0: invalid_exit()
                if j is not None:
                    y = (V[j]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not row_has_0[i]) and (not col_has_1[j]) and len(blank_rows) < 2: invalid_exit()
                        if x==1 and (not row_has_1[i]) and (not col_has_0[j]) and len(blank_rows) < 2: invalid_exit()
                        if row_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_rows.remove(i)
        if j is not None:
            blank_cols = set()

    if blank_rows or blank_cols:
        i = list(blank_rows)[0] if len(blank_rows)==1 else None
        for j,(t,v) in enumerate(zip(T,V)):
            x = (v>>k)&1
            if t==x:
                if t==0 and (not col_has_0[j]) and len(blank_rows) == 0: invalid_exit()
                if t==1 and (not col_has_1[j]) and len(blank_rows) == 0: invalid_exit()
                if i is not None:
                    y = (U[i]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not col_has_0[i]) and (not row_has_1[j]) and len(blank_cols) < 2: invalid_exit()
                        if x==1 and (not col_has_1[i]) and (not row_has_0[j]) and len(blank_cols) < 2: invalid_exit()
                        if col_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_cols.remove(j)
        if i is not None:
            blank_rows = set()

    assert len(blank_rows) != 1
    assert len(blank_cols) != 1
    for i,r in enumerate(blank_rows):
        for j,c in enumerate(blank_cols):
            tmp[r][c] = (i%2)^(j%2)
    for i,row in enumerate(tmp):
        for j,c in enumerate(row):
            if c == -1: invalid_exit()
            if c:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)

Solution (Python):
N = int(input())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

def invalid_exit():
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    row_has_0 = [0] * N
    col_has_0 = [0] * N
    row_has_1 = [0] * N
    col_has_1 = [0] * N
    def fill(i,j,bit):
        if bit == 1 - tmp[i][j]: invalid_exit()
        tmp[i][j] = bit
        if bit:
            row_has_1[i] = col_has_1[j] = 1
        else:
            row_has_0[i] = col_has_0[j] = 1
    blank_rows = set(range(N))
    blank_cols = set(range(N))
    for i,(s,u) in enumerate(zip(S,U)):
        x = (u>>k)&1
        if s==0 and x:
            for j in range(N):
                tmp[i][j] = 1
                col_has_1[j] = 1
            row_has_1[i] = 1
            blank_rows.remove(i)
        elif s and x==0:
            for j in range(N):
                tmp[i][j] = 0
                col_has_0[j] = 1
            row_has_0[i] = 1
            blank_rows.remove(i)
    for j,(t,v) in enumerate(zip(T,V)):
        x = (v>>k)&1
        if t==0 and x:
            for i in range(N):
                if tmp[i][j] == 0: invalid_exit()
                tmp[i][j] = 1
                row_has_1[i] = 1
            col_has_1[j] = 1
            blank_cols.remove(j)
        elif t and x==0:
            for i in range(N):
                if tmp[i][j] == 1: invalid_exit()
                tmp[i][j] = 0
                row_has_0[i] = 1
            col_has_0[j] = 1
            blank_cols.remove(j)

    if blank_rows or blank_cols:
        j = list(blank_cols)[0] if len(blank_cols)==1 else None
        for i,(s,u) in enumerate(zip(S,U)):
            x = (u>>k)&1
            if s==x:
                if s==0 and (not row_has_0[i]) and len(blank_cols) == 0: invalid_exit()
                if s==1 and (not row_has_1[i]) and len(blank_cols) == 0: invalid_exit()
                if j is not None:
                    y = (V[j]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not row_has_0[i]) and (not col_has_1[j]) and len(blank_rows) < 2: invalid_exit()
                        if x==1 and (not row_has_1[i]) and (not col_has_0[j]) and len(blank_rows) < 2: invalid_exit()
                        if row_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_rows.remove(i)
        if j is not None:
            blank_cols = set()

    if blank_rows or blank_cols:
        i = list(blank_rows)[0] if len(blank_rows)==1 else None
        for j,(t,v) in enumerate(zip(T,V)):
            x = (v>>k)&1
            if t==x:
                if t==0 and (not col_has_0[j]) and len(blank_rows) == 0: invalid_exit()
                if t==1 and (not col_has_1[j]) and len(blank_rows) == 0: invalid_exit()
                if i is not None:
                    y = (U[i]>>k)&1
                    if x==y:
                        fill(i,j,x)
                    else:
                        if x==0 and (not col_has_0[i]) and (not row_has_1[j]) and len(blank_cols) < 2: invalid_exit()
                        if x==1 and (not col_has_1[i]) and (not row_has_0[j]) and len(blank_cols) < 2: invalid_exit()
                        if col_has_0[i]:
                            fill(i,j,1)
                        else:
                            fill(i,j,0)
                    blank_cols.remove(j)
        if i is not None:
            blank_rows = set()

    assert len(blank_rows) != 1
    assert len(blank_cols) != 1
    for i,r in enumerate(blank_rows):
        for j,c in enumerate(blank_cols):
            tmp[r][c] = (i%2)^(j%2)
    for i,row in enumerate(tmp):
        for j,c in enumerate(row):
            if c == -1: invalid_exit()
            if c:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)

Solution (Python):
N = int(input())
S = list(map(int,input().split()))
T = list(map(int,input().split()))
U = list(map(int,input().split()))
V = list(map(int,input().split()))

def invalid_exit():
    print(-1)
    exit()

ans = [[0]*N for _ in range(N)]
for k in range(64):
    tmp = [[-1]*N for _ in range(N)]
    row_has_0 = [0] * N
    col_has_0 = [0] * N
    row_has_1 = [0] * N
    col_has_1 = [0] * N
    def fill(i,j,bit):
        if bit == 1 - tmp[i][j]: invalid_exit()
        tmp[i][j] = bit
        if bit:
            row_has_1[i] = col_has_1[j] = 1
        else:
            row_has_0[i] = col_has_0[j] = 1
    blank_rows = set(range(N))
    blank_cols = set(range(N))
    for i,(s,u) in enumerate(zip(S,U)):
        x = (u>>k)&1
        if s==0 and x:
            for j in range(N):
                tmp[i][j] = 1
                col_has_1[j] = 1
            row_has_1[i] = 1
            blank_rows.remove(i)
        elif s and x==0:
            for j in range(N):
                tmp[i][j] = 0
                col_has_0[j] = 1
            row_has_0[i] = 1
            blank_rows.remove(i)
    for j,(t,v) in enumerate(zip(T,V)):
        x = (v>>k)&1
        if t==0 and x:
            for i in range(N):
                if tmp[i][j] == 0: invalid_exit()
                tmp[i][j] = 1
                row_has_1[i] = 1
            col_has_1[j] = 1
            blank_cols.remove(j)
        elif t and x==0:
            for i in range(N):
                if tmp[i][j] == 1: invalid_exit()
                tmp[i][j] = 0
                row_has_0[i] = 1
            col_has_0[j] = 1
            blank_cols.remove(j)

    for _ in range(2):
        if blank_rows or blank_cols:
            j = list(blank_cols)[0] if len(blank_cols)==1 else None
            for i,(s,u) in enumerate(zip(S,U)):
                x = (u>>k)&1
                if s==x:
                    if s==0 and (not row_has_0[i]) and len(blank_cols) == 0: invalid_exit()
                    if s==1 and (not row_has_1[i]) and len(blank_cols) == 0: invalid_exit()
                    if j is not None:
                        y = (V[j]>>k)&1
                        if x==y:
                            fill(i,j,x)
                        else:
                            if x==0 and (not row_has_0[i]) and (not col_has_1[j]) and len(blank_rows) < 2: invalid_exit()
                            if x==1 and (not row_has_1[i]) and (not col_has_0[j]) and len(blank_rows) < 2: invalid_exit()
                            if row_has_0[i]:
                                fill(i,j,1)
                            else:
                                fill(i,j,0)
                        blank_rows.remove(i)
            if j is not None:
                blank_cols = set()

        if blank_rows or blank_cols:
            i = list(blank_rows)[0] if len(blank_rows)==1 else None
            for j,(t,v) in enumerate(zip(T,V)):
                x = (v>>k)&1
                if t==x:
                    if t==0 and (not col_has_0[j]) and len(blank_rows) == 0: invalid_exit()
                    if t==1 and (not col_has_1[j]) and len(blank_rows) == 0: invalid_exit()
                    if i is not None:
                        y = (U[i]>>k)&1
                        if x==y:
                            fill(i,j,x)
                        else:
                            if x==0 and (not col_has_0[i]) and (not row_has_1[j]) and len(blank_cols) < 2: invalid_exit()
                            if x==1 and (not col_has_1[i]) and (not row_has_0[j]) and len(blank_cols) < 2: invalid_exit()
                            if col_has_0[i]:
                                fill(i,j,1)
                            else:
                                fill(i,j,0)
                        blank_cols.remove(j)
            if i is not None:
                blank_rows = set()

    assert len(blank_rows) != 1
    assert len(blank_cols) != 1
    for i,r in enumerate(blank_rows):
        for j,c in enumerate(blank_cols):
            tmp[r][c] = (i%2)^(j%2)
    for i,row in enumerate(tmp):
        for j,c in enumerate(row):
            if c == -1: invalid_exit()
            if c:
                ans[i][j] |= (1<<k)

for row in ans:
    print(*row)