于是去弄了个板子来
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
const int N = 505;
int qpow(int p,int q) {return ((q&1)?p:1) * (q?qpow(p*p%mod,q>>1):1) % mod;}
int n, Q;
// Input: a[][],n
// Method: build() modify(i,j,x)
// Output: inv[][], ans
struct matrix {
int a[N][N], st[N][N], inv[N][N], ans, n;
void build() {
ans=1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
st[i][j]=a[i][j];
}
}
for (int i = 1; i <= n; i++) inv[i][i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
if (a[j][i]) {
for (int k = 1; k <= n; k++) {
swap(a[i][k], a[j][k]);
swap(inv[i][k], inv[j][k]);
}
if (j > i) ans = (mod - ans) % mod;
break;
}
}
int K = qpow(a[i][i], mod-2);
for (int j = i+1; j <= n; j++) {
int tmp = K * a[j][i] % mod;
for (int k = 1; k <= n; k++) {
a[j][k] = (a[j][k] + mod - tmp * a[i][k] % mod) % mod;
inv[j][k] = (inv[j][k] + mod - tmp * inv[i][k] % mod) % mod;
}
}
}
for (int i = n; i >= 1; i--) {
int K = qpow(a[i][i], mod-2);
ans = ans * a[i][i] % mod;
for (int j = 1; j <= n; j++) {
a[i][j] = a[i][j] * K % mod;
inv[i][j] = inv[i][j] * K % mod;
}
for (int j = 1; j < i; j++) {
int tmp = a[j][i];
for (int k = 1; k <= n; k++) {
a[j][k] = (a[j][k] + mod - tmp * a[i][k] % mod) % mod;
inv[j][k] = (inv[j][k] + mod - tmp * inv[i][k] % mod) % mod;
}
}
}
}
void modify(int x,int y,int z) {
int t = (z + mod - st[x][y]) % mod;
st[x][y] = z;
for (int i = 1; i <= n; i++) {
a[i][y] = (a[i][y] + t * inv[i][x] % mod) % mod;
}
{
int i = y;
int K = qpow(a[i][i], mod-2);
ans = ans * a[i][i] % mod;
for (int j = 1; j <= n; j++) {
a[i][j] = a[i][j] * K % mod;
inv[i][j] = inv[i][j] * K % mod;
}
for (int j = 1; j <= n; j++) {
if (j == i) continue;
int tmp = a[j][i];
for (int k = 1; k <= n; k++) {
a[j][k] = (a[j][k] + mod - tmp * a[i][k] % mod) % mod;
inv[j][k] = (inv[j][k] + mod - tmp * inv[i][k] % mod) % mod;
}
}
}
}
} mat;
signed main() {
scanf("%d%d", &n, &Q);
mat.n = n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &mat.a[i][j]);
}
}
mat.build();
while(Q--) {
int t1,t2,t3;
scanf("%d%d%d", &t1, &t2, &t3);
mat.modify(t1,t2,t3);
printf("%d
", mat.ans);
}
}
/*
2 3
0 1
1 0
1 1 1
2 1 2
2 2 1
[Output]
998244352
998244351
998244352
*/