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: Accepted)

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n; cin >> n;
  vector<int> s(n), t(n);
  REP(i, n) cin >> s[i];
  REP(i, n) cin >> t[i];
  using ull = unsigned long long;
  vector<ull> u(n), v(n);
  REP(i, n) cin >> u[i];
  REP(i, n) cin >> v[i];
  function<vector<vector<int>>(const vector<int>&, const vector<int>&)> solve = [&](const vector<int> &a, const vector<int> &b) {
    // REP(i, n) cout << a[i] << " \n"[i + 1 == n];
    // REP(i, n) cout << b[i] << " \n"[i + 1 == n];
    // cout << '\n';
    vector res(n, vector(n, 0));
    int zero = 0, one = 0;
    vector<int> ok, ng_zero, ng_one;
    REP(i, n) {
      REP(j, n) res[i][j] = a[i];
      if (s[i] == 0) {
        if (a[i] == 0) {
          ng_zero.emplace_back(i);
        } else {
          ++one;
        }
      } else if (s[i] == 1) {
        if (a[i] == 1) {
          ng_one.emplace_back(i);
        } else {
          ++zero;
        }
      }
    }
    function<void()> fn_zero = [&]() {
      while (!ng_zero.empty()) {
        ok.emplace_back(ng_zero.back());
        ng_zero.pop_back();
      }
    };
    function<void()> fn_one = [&]() {
      while (!ng_one.empty()) {
        ok.emplace_back(ng_one.back());
        ng_one.pop_back();
      }
    };
    REP(j, n) {
      if (t[j] == 0) {
        int bit = 1;
        REP(i, n) bit &= res[i][j];
        if (b[j] == 0) {
          if (bit == 0) {
            fn_zero();
            fn_one();
          } else if (bit == 1) {
            if (!ok.empty()) {
              res[ok.back()][j] = 0;
              fn_zero();
              fn_one();
            } else if (ng_one.empty()) {
              return vector<vector<int>>();
            } else {
              res[ng_one[0]][j] = 0;
              while (ng_one.size() >= 2) {
                ok.emplace_back(ng_one.back());
                ng_one.pop_back();
              }
            }
          }
        } else if (b[j] == 1) {
          if (bit == 0) {
            if (zero > 0) return vector<vector<int>>();
            REP(i, n) res[i][j] = 1;
            fn_one();
          } else if (bit == 1) {
            fn_zero();
            fn_one();
          }
        }
      } else if (t[j] == 1) {
        int bit = 0;
        REP(i, n) bit |= res[i][j];
        if (b[j] == 0) {
          if (bit == 0) {
            fn_zero();
            fn_one();
          } else if (bit == 1) {
            if (one > 0) return vector<vector<int>>();
            REP(i, n) res[i][j] = 0;
            fn_zero();
          }
        } else if (b[j] == 1) {
          if (bit == 0) {
            if (!ok.empty()) {
              res[ok.back()][j] = 1;
              fn_zero();
              fn_one();
            } else if (ng_zero.empty()) {
              return vector<vector<int>>();
            } else {
              res[ng_zero[0]][j] = 1;
              while (ng_zero.size() >= 2) {
                ok.emplace_back(ng_zero.back());
                ng_zero.pop_back();
              }
            }
          } else if (bit == 1) {
            fn_zero();
            fn_one();
          }
        }
      }
    }
    return ng_zero.empty() && ng_one.empty() ? res : vector<vector<int>>();
  };
  vector ans(n, vector(n, static_cast<ull>(0)));
  REP(bit, 64) {
    vector<int> a(n), b(n);
    REP(i, n) a[i] = u[i] >> bit & 1;
    REP(i, n) b[i] = v[i] >> bit & 1;
    vector<vector<int>> now = solve(a, b);
    // REP(i, n) REP(j, n) cout << now[i][j] << " \n"[j + 1 == n];
    // cout << '\n';
    if (now.empty()) {
      cout << "-1\n";
      return 0;
    }
    REP(i, n) REP(j, n) ans[i][j] |= static_cast<ull>(now[i][j]) << bit;
  }
  // REP(i, n) {
  //   ull bit = ans[i][0];
  //   if (s[i] == 0) {
  //     FOR(j, 1, n) bit &= ans[i][j];
  //   } else if (s[i] == 1) {
  //     FOR(j, 1, n) bit |= ans[i][j];
  //   }
  //   cout << bit << " \n"[i + 1 == n];
  // }
  // REP(j, n) {
  //   ull bit = ans[0][j];
  //   if (t[j] == 0) {
  //     FOR(i, 1, n) bit &= ans[i][j];
  //   } else if (t[j] == 1) {
  //     FOR(i, 1, n) bit |= ans[i][j];
  //   }
  //   cout << bit << " \n"[j + 1 == n];
  // }
  REP(i, n) REP(j, n) cout << ans[i][j] << " \n"[j + 1 == n];
  return 0;
}