#include #include using namespace std; using namespace atcoder; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) using ll = long long; const int mod = 998244353; using mint = modint998244353; using mvec = vector; using mmat = vector; #define debug(x) cout << #x << "=" << x << endl; #define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, (int)v.size()){ cout << v[i_debug] << ","; } cout << endl; } #define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, (int)m.size()){ REP(j_debug, (int)m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} } // Library Checker "Characteristic Polynomial" より引用 // https://judge.yosupo.jp/submission/68640 namespace LibraryChecker { template std::vector characteristic_polynomial(std::vector> M) { assert(M.empty() or M.size() == M[0].size()); int n = M.size(); // reduce M to upper Hessenberg form for (int j = 0; j < n - 2; j++) { for (int i = j + 2; i < n; i++) { if (M[i][j] != 0) { std::swap(M[j + 1], M[i]); for (int k = 0; k < n; k++) std::swap(M[k][j + 1], M[k][i]); break; } } if (M[j + 1][j] == 0) continue; auto inv = T(1) / M[j + 1][j]; for (int i = j + 2; i < n; i++) { auto coef = M[i][j] * inv; for (int k = j; k < n; k++) M[i][k] -= coef * M[j + 1][k]; for (int k = 0; k < n; k++) M[k][j + 1] += coef * M[k][i]; } } // compute the characteristic polynomial of upper Hessenberg matrix M std::vector> p(n + 1); p[0] = {T(1)}; for (int i = 0; i < n; i++) { p[i + 1].resize(i + 2); for (int j = 0; j <= i; j++) { p[i + 1][j + 1] += p[i][j]; p[i + 1][j] -= p[i][j] * M[i][i]; } T betas = 1; for (int j = i - 1; j >= 0; j--) { betas *= M[j + 1][j]; T coef = -betas * M[j][i]; for (int k = 0; k <= j; k++) p[i + 1][k] += coef * p[j][k]; } } return p[n]; } } // namespace LibraryChecker mmat read_matrix(int N){ mmat M(N, mvec(N)); REP(i, N) REP(j, N){ int x; cin >> x; M[i][j] = x; } return M; } void write_matrix(mmat M){ int N = M.size(); REP(i, N){ REP(j, N) cout << M[i][j].val() << ','; cout << endl; } } //swap M[i,:] and M[j,:] void swap_row(mmat &M, int i, int j){ assert(i != j); M[i].swap(M[j]); } //swap M[:,i] and M[:,j] void swap_column(mmat &M, int i, int j){ assert(i != j); int N = M.size(); REP(k, N) swap(M[k][i], M[k][j]); } //M[i,:]-=a*M[j:] void sbt_row(mmat &M, int i, int j, mint a){ assert(i != j); int N = M.size(); REP(k, N) M[i][k] -= a * M[j][k]; } //M[:,i]-=a*M[:,j] void sbt_column(mmat &M, int i, int j, mint a){ assert(i != j); int N = M.size(); REP(k, N) M[k][i] -= a * M[k][j]; } //M[i,:]*=a; void mult_row(mmat &M, int i, mint a){ int N = M.size(); REP(k, N) M[i][k] *= a; } mvec solve(mmat M0, mmat M1){ int N = M0.size(); // 最後にf0を行列式にかける mint f0 = 1; // 上d行と左d列では、Bの要素は対角上の1のみになるようにしていく int d = 0; while(d < N){ // M1のd列目から非0要素を探す、上d行は0なので探さなくてよい int i0 = -1; FOR(i, d, N) if(M1[i][d].val() != 0){ i0 = i; break; } if(i0 == -1){ // M1は正則でない return mvec(N + 1, 0); } if(i0 != d){ // 行交換 f0 *= -1; swap_row(M0, i0, d); swap_row(M1, i0, d); } // M1[d][d]=1にする f0 *= M1[d][d]; mint r = (mint)1 / M1[d][d]; mult_row(M0, d, r); mult_row(M1, d, r); assert(M1[d][d].val() == 1); // d行目とd列目から他のx消去 FOR(i, d + 1, N){ mint a = M1[i][d]; sbt_row(M0, i, d, a); sbt_row(M1, i, d, a); } FOR(j, d + 1, N){ mint a = M1[d][j]; sbt_column(M0, j, d, a); sbt_column(M1, j, d, a); } d++; } // M1は単位行列になっているはず REP(i, N) REP(j, N){ assert(M1[i][j].val() == (i == j)); } // 答えは det(xI-(-M0))*f0 REP(i, N) REP(j, N) M0[i][j] *= -1; auto cp = LibraryChecker::characteristic_polynomial(M0); mvec ans(N + 1); REP(i, N + 1) ans[i] = cp[i] * f0; return ans; } int main(){ int N; cin >> N; auto M0 = read_matrix(N); auto M1 = read_matrix(N); mint a = 63504649; // x=y-a とする // det(M0+xM1)=det((M0-aM1)+yM1)=y^N det((M0-aM1)y^(-1)+M1) // M0+xM1が正則ならばM0-aM1は高い確率で正則 // det((M0-aM1)y^(-1)+M1)をy^(-1)の多項式として求める mmat M0_2 = M0; REP(i, N) REP(j, N) M0_2[i][j] -= a * M1[i][j]; auto poly_y = solve(M1, M0_2); // y^N を乗じ、yの多項式に変換 REP(i, N + 1) if(i < N - i) swap(poly_y[i], poly_y[N - i]); // y=x+aを代入して答えを得る // 二項係数 mmat ncr(N + 1, mvec(N + 1)); REP(i, N + 1){ ncr[i][0] = ncr[i][i] = 1; FOR(j, 1, i) ncr[i][j] = ncr[i - 1][j - 1] + ncr[i - 1][j]; } mvec poly_x(N + 1); REP(i, N + 1) REP(j, i + 1){ poly_x[j] += poly_y[i] * ncr[i][j] * a.pow(i - j); } REP(i, N + 1) cout << poly_x[i].val() << endl; return 0; }