Problem: 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.

Submission attempts (in order):
import os

import sys
import numpy as np

from libs.debug import debug

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
row_or = list(map(int, sys.stdin.buffer.readline().split()))
col_or = list(map(int, sys.stdin.buffer.readline().split()))
right = list(map(int, sys.stdin.buffer.readline().split()))
down = list(map(int, sys.stdin.buffer.readline().split()))

row_or = np.array(row_or, dtype=bool)
row_and = ~row_or
col_or = np.array(col_or, dtype=bool)
col_and = ~col_or
right = np.array(right, dtype=int)
down = np.array(down, dtype=int)


@debug
def solve(right, down):
    ret = np.full((N, N), -1, dtype=int)
    yet_rows = []
    yet_cols = []
    used = [False] * 2
    for r in range(N):
        if row_or[r] and right[r] == 0:
            ret[r, :] = 0
            used[0] = True
        elif row_and[r] and right[r] == 1:
            ret[r, :] = 1
            used[1] = True
        else:
            yet_rows.append(r)
    for c in range(N):
        if col_or[c] and down[c] == 0:
            ret[:, c] = 0
            used[0] = True
        elif col_and[c] and down[c] == 1:
            ret[:, c] = 1
            used[1] = True
        else:
            yet_cols.append(c)

    if len(yet_cols) == 1:
        c = yet_cols[0]
        if not used[0]:
            
            ret[yet_rows, c] = 0
            for r in yet_rows:
                
                
                if row_or[r] and right[r] == 1:
                    ret[r, c] = 1
                    break
        elif not used[1]:
            
            ret[yet_rows, c] = 1
            for r in yet_rows:
                
                
                if row_and[r] and right[r] == 0:
                    ret[r, c] = 0
                    break
        else:
            
            ret[yet_rows, c] = np.arange(len(yet_rows), dtype=int) % 2
    elif len(yet_rows) == 1:
        r = yet_rows[0]
        if not used[0]:
            
            ret[r, yet_cols] = 0
            for c in yet_cols:
                
                
                if col_or[c] and down[c] == 1:
                    ret[r, c] = 1
                    break
        elif not used[1]:
            
            ret[r, yet_cols] = 1
            for c in yet_cols:
                
                
                if col_and[c] and down[c] == 0:
                    ret[r, c] = 0
                    break
        else:
            
            ret[r, yet_cols] = np.arange(len(yet_cols), dtype=int) % 2
    else:
        
        for i, r in enumerate(yet_rows):
            ret[r, yet_cols] = (np.arange(len(yet_cols), dtype=int) + i) % 2

    
    ok = True
    for r in range(N):
        if row_and[r]:
            if right[r] == 1:
                ok &= np.all(ret[r, :] == 1)
            else:
                ok &= np.any(ret[r, :] == 0)
        else:
            if right[r] == 1:
                ok &= np.any(ret[r, :] == 1)
            else:
                ok &= np.all(ret[r, :] == 0)
    for c in range(N):
        if col_and[c]:
            if down[c] == 1:
                ok &= np.all(ret[:, c] == 1)
            else:
                ok &= np.any(ret[:, c] == 0)
        else:
            if down[c] == 1:
                ok &= np.any(ret[:, c] == 1)
            else:
                ok &= np.all(ret[:, c] == 0)
    return ret, ok


ans = np.zeros((N, N), dtype=int)
for i in range(64):
    bits, ok = solve(right >> i & 1, down >> i & 1)
    if not ok:
        print(-1)
        exit()
    ans |= bits << i

for row in ans:
    print(*row)


import os

import sys
import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
row_or = list(map(int, sys.stdin.buffer.readline().split()))
col_or = list(map(int, sys.stdin.buffer.readline().split()))
right = list(map(int, sys.stdin.buffer.readline().split()))
down = list(map(int, sys.stdin.buffer.readline().split()))

row_or = np.array(row_or, dtype=bool)
row_and = ~row_or
col_or = np.array(col_or, dtype=bool)
col_and = ~col_or
right = np.array(right, dtype='uint64')
down = np.array(down, dtype='uint64')


def solve(right, down):
    ret = np.full((N, N), 7, dtype='uint64')
    yet_rows = []
    yet_cols = []
    used = [False] * 2
    for r in range(N):
        if row_or[r] and right[r] == 0:
            ret[r, :] = 0
            used[0] = True
        elif row_and[r] and right[r] == 1:
            ret[r, :] = 1
            used[1] = True
        else:
            yet_rows.append(r)
    for c in range(N):
        if col_or[c] and down[c] == 0:
            ret[:, c] = 0
            used[0] = True
        elif col_and[c] and down[c] == 1:
            ret[:, c] = 1
            used[1] = True
        else:
            yet_cols.append(c)

    if len(yet_cols) == 1:
        c = yet_cols[0]
        if not used[0]:
            
            ret[yet_rows, c] = 0
            if down[c] == 1:
                for r in yet_rows:
                    
                    
                    if row_or[r] and right[r] == 1:
                        ret[r, c] = 1
                        break
        elif not used[1]:
            
            ret[yet_rows, c] = 1
            if down[c] == 0:
                for r in yet_rows:
                    
                    
                    if row_and[r] and right[r] == 0:
                        ret[r, c] = 0
                        break
        else:
            
            ret[yet_rows, c] = np.arange(len(yet_rows), dtype=int) % 2
    elif len(yet_rows) == 1:
        r = yet_rows[0]
        if not used[0]:
            
            ret[r, yet_cols] = 0
            if right[r] == 1:
                for c in yet_cols:
                    
                    
                    if col_or[c] and down[c] == 1:
                        ret[r, c] = 1
                        break
        elif not used[1]:
            
            ret[r, yet_cols] = 1
            if right[r] == 0:
                for c in yet_cols:
                    
                    
                    if col_and[c] and down[c] == 0:
                        ret[r, c] = 0
                        break
        else:
            
            ret[r, yet_cols] = np.arange(len(yet_cols), dtype=int) % 2
    else:
        
        for i, r in enumerate(yet_rows):
            ret[r, yet_cols] = (np.arange(len(yet_cols), dtype=int) + i) % 2

    
    ok = True
    for r in range(N):
        if row_and[r]:
            if right[r] == 1:
                ok &= np.all(ret[r, :] == 1)
            else:
                ok &= np.any(ret[r, :] == 0)
        else:
            if right[r] == 1:
                ok &= np.any(ret[r, :] == 1)
            else:
                ok &= np.all(ret[r, :] == 0)
    for c in range(N):
        if col_and[c]:
            if down[c] == 1:
                ok &= np.all(ret[:, c] == 1)
            else:
                ok &= np.any(ret[:, c] == 0)
        else:
            if down[c] == 1:
                ok &= np.any(ret[:, c] == 1)
            else:
                ok &= np.all(ret[:, c] == 0)
    assert np.all(ret <= 1)
    return ret & 1, ok


ans = np.zeros((N, N), dtype='uint64')
for i in range(64):
    bits, ok = solve(right >> i & 1, down >> i & 1)
    if not ok:
        print(-1)
        exit()
    ans |= bits << i

for row in ans:
    print(*row)


Verdicts: Runtime Error

Accepted