int n; int a[MAXn + 10][MAXn + 10]; bool neg; int mul; inlinevoidswaprow(int x, int y, int bottomj){ for (int j = bottomj; j <= n; ++j) { swap(a[x][j], a[y][j]); } neg ^= 1; } inlinevoidmulrow(int x, int v, int bottomj){ for (int j = bottomj; j <= n; ++j) { a[x][j] = a[x][j] * v % mod; } mul = mul * v % mod; } inlinevoidredrow(int x, int y, int bottomj){ // x -= y for (int j = bottomj; j <= n; ++j) { a[x][j] = redmod(a[x][j] - a[y][j]); } } intdet(){ neg = 0; mul = 1; for (int rc = 1; rc <= n; ++rc) { int choser = -1; for (int i = rc; i <= n; ++i) { if (a[i][rc]) { choser = i; break; } } if (choser == -1) return0; if (choser != rc) swaprow(rc, choser, rc); for (int i = rc + 1; i <= n; ++i) { if (a[i][rc]) { mulrow(i, a[rc][rc] * inv(a[i][rc]) % mod, rc); redrow(i, rc, rc); } } } int ans = 1; for (int i = 1; i <= n; ++i) { ans = ans * a[i][i] % mod; } if (neg) ans = redmod(-ans); ans = ans * inv(mul) % mod; return ans; }
int n; int a[MAXn + 10][MAXn + 10]; bool neg; inlinevoidswaprow(int x, int y, int bottomj){ for (int j = bottomj; j <= n; ++j) { swap(a[x][j], a[y][j]); } neg ^= 1; } inlinevoidredrow(int x, int y, int v, int bottomj){ // x -= y * v for (int j = bottomj; j <= n; ++j) { a[x][j] = redmod(a[x][j] - a[y][j] * v % mod); } } intdet(){ neg = 0; for (int rc = 1; rc <= n; ++rc) { int choser = -1; for (int i = rc; i <= n; ++i) { if (a[i][rc]) { choser = i; break; } } if (choser == -1) return0; if (choser != rc) swaprow(rc, choser, rc); for (int i = rc + 1; i <= n; ++i) { if (a[i][rc]) { if (a[i][rc] > a[rc][rc]) swaprow(rc, i, rc); while (a[i][rc]) { redrow(rc, i, a[rc][rc] / a[i][rc], rc); swaprow(rc, i, rc); } } } } int ans = 1; for (int i = 1; i <= n; ++i) { ans = ans * a[i][i] % mod; } if (neg) ans = redmod(-ans); return ans; }
#include<bits/stdc++.h> usingnamespace std; #define int long long constint MAXn = 6e2;
template <typename T> inlinevoidread(T &a){ char c;for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar());bool f = c == '-';T x = f ? 0 : (c ^ '0');for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {x = x * 10 + (c ^ '0');}a = f ? -x : x; } template <typename T, typename ...Argv> inlinevoidread(T &a, Argv &...argv){ read(a), read(argv...); }
int mod; inlineintredmod(int x){ return (x < 0) ? x + mod : x; }
inlineintpower(int x, int y){ int ans = 1; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } inlineintinv(int x){ returnpower(x, mod - 2); }
int n; int a[MAXn + 10][MAXn + 10]; bool neg; inlinevoidswaprow(int x, int y, int bottomj){ for (int j = bottomj; j <= n; ++j) { swap(a[x][j], a[y][j]); } neg ^= 1; } inlinevoidredrow(int x, int y, int v, int bottomj){ // x -= y * v for (int j = bottomj; j <= n; ++j) { a[x][j] = redmod(a[x][j] - a[y][j] * v % mod); } } intdet(){ neg = 0; for (int rc = 1; rc <= n; ++rc) { int choser = -1; for (int i = rc; i <= n; ++i) { if (a[i][rc]) { choser = i; break; } } if (choser == -1) return0; if (choser != rc) swaprow(rc, choser, rc); for (int i = rc + 1; i <= n; ++i) { if (a[i][rc]) { if (a[i][rc] > a[rc][rc]) swaprow(rc, i, rc); while (a[i][rc]) { redrow(rc, i, a[rc][rc] / a[i][rc], rc); swaprow(rc, i, rc); } } } } int ans = 1; for (int i = 1; i <= n; ++i) { ans = ans * a[i][i] % mod; } if (neg) ans = redmod(-ans); return ans; }
signedmain(){ read(n, mod); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { read(a[i][j]); a[i][j] %= mod; } } printf("%lld\n", det()); return0; }