#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;
void out(vl v) {
for (int i = 0; i < v.size(); ++i)
cout << v[i] << ' ';
cout << endl;
}
void out(vvl v) {
for (int i = 0; i < v.size(); ++i)
out(v[i]);
cout << endl;
}
void outerr(vl v) {
for (int i = 0; i < v.size(); ++i)
cerr << v[i] << ' ';
cerr << endl;
}
void outerr(vvl v) {
for (int i = 0; i < v.size(); ++i)
outerr(v[i]);
cerr << endl;
}
vvl id (int n) {
vvl v(n, vl(n));
for (int i =0 ; i < n; ++i) v[i][i] = 1;
return v;
}
void model(vvl & v, vl x, int inv) {
if (x.size() == 2) {
if (x[1] == -1) {
for (int j = 0; j < v.size(); ++j)
v[x[0]][j] *= -1;
return;
}
model(v, {x[0], x[1], 1}, 0);
model(v, {x[1], x[0], -1}, 0);
model(v, {x[0], x[1], 1}, 0);
model(v, {x[0], -1}, 0);
return;
}
if (inv) {
x[2] *= -1;
}
for (int j = 0; j < v.size(); ++j)
v[x[1]][j] += v[x[0]][j]*x[2];
}
vvl tr(vvl v) {
int n = v.size();
for (int i = 0; i < n; ++i) for (int j = 0; j < i; ++j) {
swap(v[i][j], v[j][i]);
}
return v;
}
vvl mul(vvl x, vvl y) {
int n = x.size();
vvl res(n, vl(n));
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
for (int l = 0; l < n; ++l) {
//if (abs(y[l][j])) assert(abs(x[i][l]) < 4e18/abs(y[l][j]));
res[i][j] += x[i][l]*y[l][j];
//assert(abs(res[i][j]) < 4e18);
}
}
return res;
}
int main() {
freopen("higher.in", "r", stdin);
freopen("higher.out", "w", stdout);
int n = 4;
for (int mask = 0; mask < 100000; ++mask) {
int m = mask;
}while (cin >> n) {
if (n == 0) break;
vvl a(n, vl(n));
for (int i = 0; i < n; ++i) for (int j =0 ; j < n; ++j) {
//a[i][j] = m % 4 - 2;
//m /= 4;
cin >> a[i][j];
}
vvl a0 = a;
int b = 0;
vvl row, col;
while (b < n) {
//outerr (a);
// out (a);
ll mi=2.1e18;
int r,c;
for (int i = b; i < n; ++i) for (int j = b; j < n; ++j) {
if (a[i][j] && abs(a[i][j]) < mi) {
r=i; c=j;
mi = abs(a[i][j]);
}
}
if (mi > 2e18) break;
int r1 = -1, c1 = -1;
ll m1 = mi;
for (int i = b; i < n; ++i) for (int j = b; j < n; ++j) if (abs(a[i][j])) {
for (int i1 = b; i1 < n; ++i1) if (i1 != i && abs(a[i1][j]) % abs(a[i][j])) {
if (abs(a[i1][j]) % abs(a[i][j]) < m1) {
m1 = abs(a[i1][j]) % abs(a[i][j]);
r = i;
r1 = i1;
c = c1 = j;
}
}
for (int j1 = b; j1 < n; ++j1) if (j1 != j && abs(a[i][j1]) % abs(a[i][j])) {
if (abs(a[i][j1]) % abs(a[i][j]) < m1) {
m1 = abs(a[i][j1]) % abs(a[i][j]);
r = r1 = i;
c = j;
c1 = j1;
}
}
}
if (m1 == mi) {
for (int i = b; i < n; ++i) for (int j = b; j < n; ++j) if (abs(a[i][j])) {
for (int i1 = b; i1 < n; ++i1) for (int j1 = b; j1 < n; ++j1) if (abs(a[i1][j1]) % abs(a[i][j])) {
if (abs(a[i1][j1]) % abs(a[i][j]) < m1) {
m1 = abs(a[i1][j1]) % abs(a[i][j]);
r = i;
r1 = i1;
c = j;
c1 = j1;
}
}
}
}
if (r1 == -1) {
if (b != r) {
row.push_back({b, r});
model(a, row.back(), 0);
//a[b].swap(a[r]);
}
if (b != c) {
col.push_back({b, c});
for (int i = 0; i < n; ++i)
swap(a[i][b], a[i][c]);
}
for (int i = b + 1; i < n; ++i) {
row.push_back({b, i, -a[i][b] / a[b][b]});
model(a, row.back(), 0);
assert(a[i][b] == 0);
}
for (int i = b + 1; i < n; ++i) {
ll x = -a[b][i] / a[b][b];
col.push_back({b, i, x});
for (int j = 0; j < n; ++j) {
a[j][i] += a[j][b]*x;
}
assert(a[b][i] == 0);
}
if (a[b][b] < 0) {
row.push_back({b, -1});
a[b][b] *= -1;
}
++b;
} else {
assert(abs(a[r1][c1]) % abs(a[r][c]) == m1);
if (r != r1 && c != c1) {
assert(a[r1][c] % abs(a[r][c]) == 0);
row.push_back({r, r1, -a[r1][c] / a[r][c] + 1});
r = r1;
model(a, row.back(), 0);
}
if (a[r][c] < 0) {
row.push_back({r, -1});
model(a, row.back(), 0);
}
if (a[r1][c1] < 0) {
row.push_back({r1, -1});
model(a, row.back(), 0);
}
if (r == r1) {
ll x = -a[r][c1]/a[r][c]; //??
col.push_back({c, c1, x});
for (int i = 0; i < n; ++i)
a[i][c1] += a[i][c]*x;
} else {
assert(c == c1);
ll x = -a[r1][c]/a[r][c]; //??
row.push_back({r, r1, x});
for (int i = 0; i < n; ++i)
a[r1][i] += a[r][i]*x;
}
//assert(abs(a[r1][c1]) == m1);
}
}
vvl L = id(n), L1 = id(n), R = id(n), R1 = id(n);
vvl test = a0;
for (int i = 0; i < row.size(); ++i) {
model(L1, row[row.size()-i-1], 1);
model(L, row[i], 0);
model(test, row[i], 0);
}
assert(test == mul(L, a0));
for (int i = 0; i < col.size(); ++i) {
model(R, col[i], 0);
model(R1, col[col.size()-i-1], 1);
}
R = tr(R);
R1 = tr(R1);
out(L);
out(L1);
out(R);
out(R1);
assert(mul(L, L1) == id(n));
assert(mul(R, R1) == id(n));
assert(mul(mul(L, a0),R) == a);
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (i != j)
assert(a[i][j] == 0);
for (int i = 0; i+1 < n; ++i) {
assert(a[i][i] == 0 || a[i+1][i+1] % a[i][i] == 0);
assert(a[i][i] || a[i+1][i+1] == 0);
}
}
return 0;
}
I2lu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sraikgewogICAgICAgIHN3YXAodltpXVtqXSwgdltqXVtpXSk7CiAgICB9CiAgICByZXR1cm4gdjsKfQoKdnZsIG11bCh2dmwgeCwgdnZsIHkpIHsKICAgIGludCBuID0geC5zaXplKCk7CiAgICB2dmwgcmVzKG4sIHZsKG4pKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSBmb3IgKGludCBqID0gMDsgaiA8IG47ICsraikgewogICAgICAgIGZvciAoaW50IGwgPSAwOyBsIDwgbjsgKytsKSB7CiAgICAgICAgICAgIC8vaWYgKGFicyh5W2xdW2pdKSkgYXNzZXJ0KGFicyh4W2ldW2xdKSA8IDRlMTgvYWJzKHlbbF1bal0pKTsKICAgICAgICAgICAgcmVzW2ldW2pdICs9IHhbaV1bbF0qeVtsXVtqXTsKICAgICAgICAgICAgLy9hc3NlcnQoYWJzKHJlc1tpXVtqXSkgPCA0ZTE4KTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gcmVzOwp9CgppbnQgbWFpbigpIHsKICAgIGZyZW9wZW4oImhpZ2hlci5pbiIsICJyIiwgc3RkaW4pOwogICAgZnJlb3BlbigiaGlnaGVyLm91dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIGludCBuID0gNDsKICAgIGZvciAoaW50IG1hc2sgPSAwOyBtYXNrIDwgMTAwMDAwOyArK21hc2spIHsKICAgICAgICBpbnQgbSA9IG1hc2s7CiAgICB9d2hpbGUgKGNpbiA+PiBuKSB7CiAgICAgICAgaWYgKG4gPT0gMCkgYnJlYWs7CiAgICAgICAgdnZsIGEobiwgdmwobikpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSBmb3IgKGludCBqID0wIDsgaiA8IG47ICsraikgewogICAgICAgICAgICAvL2FbaV1bal0gPSBtICUgNCAtIDI7CiAgICAgICAgICAgIC8vbSAvPSA0OwogICAgICAgICAgICBjaW4gPj4gYVtpXVtqXTsKICAgICAgICB9CiAgICAgICAgdnZsIGEwID0gYTsKICAgICAgICBpbnQgYiA9IDA7CiAgICAgICAgdnZsIHJvdywgY29sOwogICAgICAgIHdoaWxlIChiIDwgbikgewogICAgICAgICAgICAvL291dGVyciAoYSk7CiAgICAgICAgICAgIC8vCQkJb3V0IChhKTsKICAgICAgICAgICAgbGwgbWk9Mi4xZTE4OwogICAgICAgICAgICBpbnQgcixjOwogICAgICAgICAgICBmb3IgKGludCBpID0gYjsgaSA8IG47ICsraSkgZm9yIChpbnQgaiA9IGI7IGogPCBuOyArK2opIHsKICAgICAgICAgICAgICAgIGlmIChhW2ldW2pdICYmIGFicyhhW2ldW2pdKSA8IG1pKSB7CiAgICAgICAgICAgICAgICAgICAgcj1pOyBjPWo7CiAgICAgICAgICAgICAgICAgICAgbWkgPSBhYnMoYVtpXVtqXSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKG1pID4gMmUxOCkgYnJlYWs7CiAgICAgICAgICAgIGludCByMSA9IC0xLCBjMSA9IC0xOwogICAgICAgICAgICBsbCBtMSA9IG1pOwogICAgICAgICAgICBmb3IgKGludCBpID0gYjsgaSA8IG47ICsraSkgZm9yIChpbnQgaiA9IGI7IGogPCBuOyArK2opIGlmIChhYnMoYVtpXVtqXSkpIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGkxID0gYjsgaTEgPCBuOyArK2kxKSBpZiAoaTEgIT0gaSAmJiBhYnMoYVtpMV1bal0pICUgYWJzKGFbaV1bal0pKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGFicyhhW2kxXVtqXSkgJSBhYnMoYVtpXVtqXSkgPCBtMSkgewogICAgICAgICAgICAgICAgICAgICAgICBtMSA9IGFicyhhW2kxXVtqXSkgJSBhYnMoYVtpXVtqXSk7CiAgICAgICAgICAgICAgICAgICAgICAgIHIgPSBpOwogICAgICAgICAgICAgICAgICAgICAgICByMSA9IGkxOyAKICAgICAgICAgICAgICAgICAgICAgICAgYyA9IGMxID0gajsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmb3IgKGludCBqMSA9IGI7IGoxIDwgbjsgKytqMSkgaWYgKGoxICE9IGogJiYgYWJzKGFbaV1bajFdKSAlIGFicyhhW2ldW2pdKSkgewogICAgICAgICAgICAgICAgICAgIGlmIChhYnMoYVtpXVtqMV0pICUgYWJzKGFbaV1bal0pIDwgbTEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgbTEgPSBhYnMoYVtpXVtqMV0pICUgYWJzKGFbaV1bal0pOwogICAgICAgICAgICAgICAgICAgICAgICByID0gcjEgPSBpOwogICAgICAgICAgICAgICAgICAgICAgICBjID0gajsKICAgICAgICAgICAgICAgICAgICAgICAgYzEgPSBqMTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKG0xID09IG1pKSB7CiAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gYjsgaSA8IG47ICsraSkgZm9yIChpbnQgaiA9IGI7IGogPCBuOyArK2opIGlmIChhYnMoYVtpXVtqXSkpIHsKICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBpMSA9IGI7IGkxIDwgbjsgKytpMSkgZm9yIChpbnQgajEgPSBiOyBqMSA8IG47ICsrajEpIGlmIChhYnMoYVtpMV1bajFdKSAlIGFicyhhW2ldW2pdKSkgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAoYWJzKGFbaTFdW2oxXSkgJSBhYnMoYVtpXVtqXSkgPCBtMSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgbTEgPSBhYnMoYVtpMV1bajFdKSAlIGFicyhhW2ldW2pdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHIgPSBpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcjEgPSBpMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGMgPSBqOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYzEgPSBqMTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAocjEgPT0gLTEpIHsKICAgICAgICAgICAgICAgIGlmIChiICE9IHIpIHsKICAgICAgICAgICAgICAgICAgICByb3cucHVzaF9iYWNrKHtiLCByfSk7CiAgICAgICAgICAgICAgICAgICAgbW9kZWwoYSwgcm93LmJhY2soKSwgMCk7CiAgICAgICAgICAgICAgICAgICAgLy9hW2JdLnN3YXAoYVtyXSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoYiAhPSBjKSB7CiAgICAgICAgICAgICAgICAgICAgY29sLnB1c2hfYmFjayh7YiwgY30pOwogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKQogICAgICAgICAgICAgICAgICAgICAgICBzd2FwKGFbaV1bYl0sIGFbaV1bY10pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IGIgKyAxOyBpIDwgbjsgKytpKSB7CiAgICAgICAgICAgICAgICAgICAgcm93LnB1c2hfYmFjayh7YiwgaSwgLWFbaV1bYl0gLyBhW2JdW2JdfSk7CiAgICAgICAgICAgICAgICAgICAgbW9kZWwoYSwgcm93LmJhY2soKSwgMCk7CiAgICAgICAgICAgICAgICAgICAgYXNzZXJ0KGFbaV1bYl0gPT0gMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBmb3IgKGludCBpID0gYiArIDE7IGkgPCBuOyArK2kpIHsKICAgICAgICAgICAgICAgICAgICBsbCB4ID0gLWFbYl1baV0gLyBhW2JdW2JdOwogICAgICAgICAgICAgICAgICAgIGNvbC5wdXNoX2JhY2soe2IsIGksIHh9KTsKICAgICAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG47ICsraikgewogICAgICAgICAgICAgICAgICAgICAgICBhW2pdW2ldICs9IGFbal1bYl0qeDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgYXNzZXJ0KGFbYl1baV0gPT0gMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAoYVtiXVtiXSA8IDApIHsKICAgICAgICAgICAgICAgICAgICByb3cucHVzaF9iYWNrKHtiLCAtMX0pOwogICAgICAgICAgICAgICAgICAgIGFbYl1bYl0gKj0gLTE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICArK2I7CQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgYXNzZXJ0KGFicyhhW3IxXVtjMV0pICUgYWJzKGFbcl1bY10pID09IG0xKTsKICAgICAgICAgICAgICAgIGlmIChyICE9IHIxICYmIGMgIT0gYzEpIHsKICAgICAgICAgICAgICAgICAgICBhc3NlcnQoYVtyMV1bY10gJSBhYnMoYVtyXVtjXSkgPT0gMCk7CiAgICAgICAgICAgICAgICAgICAgcm93LnB1c2hfYmFjayh7ciwgcjEsIC1hW3IxXVtjXSAvIGFbcl1bY10gKyAxfSk7CiAgICAgICAgICAgICAgICAgICAgciA9IHIxOwogICAgICAgICAgICAgICAgICAgIG1vZGVsKGEsIHJvdy5iYWNrKCksIDApOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKGFbcl1bY10gPCAwKSB7CiAgICAgICAgICAgICAgICAgICAgcm93LnB1c2hfYmFjayh7ciwgLTF9KTsKICAgICAgICAgICAgICAgICAgICBtb2RlbChhLCByb3cuYmFjaygpLCAwKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChhW3IxXVtjMV0gPCAwKSB7CiAgICAgICAgICAgICAgICAgICAgcm93LnB1c2hfYmFjayh7cjEsIC0xfSk7CiAgICAgICAgICAgICAgICAgICAgbW9kZWwoYSwgcm93LmJhY2soKSwgMCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAociA9PSByMSkgewogICAgICAgICAgICAgICAgICAgIGxsIHggPSAtYVtyXVtjMV0vYVtyXVtjXTsgLy8/PwogICAgICAgICAgICAgICAgICAgIGNvbC5wdXNoX2JhY2soe2MsIGMxLCB4fSk7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpCiAgICAgICAgICAgICAgICAgICAgICAgIGFbaV1bYzFdICs9IGFbaV1bY10qeDsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgYXNzZXJ0KGMgPT0gYzEpOwogICAgICAgICAgICAgICAgICAgIGxsIHggPSAtYVtyMV1bY10vYVtyXVtjXTsgLy8/PwogICAgICAgICAgICAgICAgICAgIHJvdy5wdXNoX2JhY2soe3IsIHIxLCB4fSk7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpCiAgICAgICAgICAgICAgICAgICAgICAgIGFbcjFdW2ldICs9IGFbcl1baV0qeDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIC8vYXNzZXJ0KGFicyhhW3IxXVtjMV0pID09IG0xKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB2dmwgTCA9IGlkKG4pLCBMMSA9IGlkKG4pLCBSID0gaWQobiksIFIxID0gaWQobik7CiAgICAgICAgdnZsIHRlc3QgPSBhMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHJvdy5zaXplKCk7ICsraSkgewogICAgICAgICAgICBtb2RlbChMMSwgcm93W3Jvdy5zaXplKCktaS0xXSwgMSk7CiAgICAgICAgICAgIG1vZGVsKEwsIHJvd1tpXSwgMCk7CiAgICAgICAgICAgIG1vZGVsKHRlc3QsIHJvd1tpXSwgMCk7CiAgICAgICAgfQogICAgICAgIGFzc2VydCh0ZXN0ID09IG11bChMLCBhMCkpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgY29sLnNpemUoKTsgKytpKSB7CiAgICAgICAgICAgIG1vZGVsKFIsIGNvbFtpXSwgMCk7CiAgICAgICAgICAgIG1vZGVsKFIxLCBjb2xbY29sLnNpemUoKS1pLTFdLCAxKTsKICAgICAgICB9CiAgICAgICAgUiA9IHRyKFIpOwogICAgICAgIFIxID0gdHIoUjEpOwogICAgICAgIG91dChMKTsKICAgICAgICBvdXQoTDEpOwogICAgICAgIG91dChSKTsKICAgICAgICBvdXQoUjEpOwogICAgICAgIGFzc2VydChtdWwoTCwgTDEpID09IGlkKG4pKTsKICAgICAgICBhc3NlcnQobXVsKFIsIFIxKSA9PSBpZChuKSk7CiAgICAgICAgYXNzZXJ0KG11bChtdWwoTCwgYTApLFIpID09IGEpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSBmb3IgKGludCBqID0gMDsgaiA8IG47ICsraikgaWYgKGkgIT0gaikKICAgICAgICAgICAgYXNzZXJ0KGFbaV1bal0gPT0gMCk7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkrMSA8IG47ICsraSkgewogICAgICAgICAgICBhc3NlcnQoYVtpXVtpXSA9PSAwIHx8IGFbaSsxXVtpKzFdICUgYVtpXVtpXSA9PSAwKTsKICAgICAgICAgICAgYXNzZXJ0KGFbaV1baV0gfHwgYVtpKzFdW2krMV0gPT0gMCk7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0K