Problem:
Takahashi has an N \times N grid. The square at the i-th row and the j-th column of the grid is denoted by (i,j). Particularly, the top-left square of the grid is (1,1), and the bottom-right square is (N,N).  An integer, 0 or 1, is written on M of the squares in the Takahashi's grid. Three integers a_i,b_i and c_i describe the i-th of those squares with integers written on them: the integer c_i is written on the square (a_i,b_i).  Takahashi decides to write an integer, 0 or 1, on each of the remaining squares so that the condition below is satisfied. Find the number of such ways to write integers, modulo 998244353.    * For all 1\leq i < j\leq N, there are even number of 1s in the square region whose top-left square is (i,i) and whose bottom-right square is (j,j).

Incorrect submission (verdict: Wrong Answer):
from collections import Counter
import sys
MOD = 998244353
n, m = map(int, input().split())
decided = Counter()
ans = 1
d_num = 0

def kaijou(a, n):
	x = 1
	while n > 0:
		if n & 1 == 1:
			x = (x * a) % MOD
		a = (a ** 2) % MOD
		n >>= 1
	return x

for _ in range(m):
	a, b, c = map(int, input().split())
	decided[(a-1) * n + b - 1] = c
	if abs(a-b) >= 3:
		if (b-1) * n + a - 1 not in decided:
			d_num += 1
			ans = (ans * 2) % MOD
			decided[(b-1) * n + a - 1] = c
		elif decided[(b-1) * n + a - 1] != decided[(a-1) * n + b - 1]:
			print(0)
			sys.exit()

ans = (ans * kaijou(2, ((n-2) * (n-3) // 2) - d_num)) % MOD

#print(ans)
for i in range(n-2):
	is_empty = 0
	if n*i+i not in decided:
		is_empty += 1
	if n*i+i+1 not in decided:
		is_empty += 1
	if n*i+i+n not in decided:
		is_empty += 1
	if n*i+i+n+1 not in decided:
		is_empty += 1

	if is_empty == 0:
		if (decided[n*i+i] + decided[n*i+i+n+1]) % 2 != (decided[n*i+i+1] + decided[n*i+i+n]) % 2:
			ans *= 0

	if is_empty == 1 and n*i+i+n+1 not in decided:
		decided[n*i+i+n+1] = (decided[n*i+i+1] + decided[n*i+i+n]) % 2 - decided[n*i+i]

	ans = (ans * (2 ** max(0, is_empty-1))) % MOD

	is_empty = 0
	if n*i+i+n+1 not in decided:
		is_empty += 1
	if n*i+i+2 not in decided:
		is_empty += 1
	if n*i+i+2*n not in decided:
		is_empty += 1

	if is_empty == 0:
		if (decided[n*i+i+2] + decided[n*i+i+n+1] + decided[n*i+i+2*n]) % 2 != 0:
			ans *= 0

	if is_empty == 1 and n*i+i+n+1 not in decided:
		decided[n*i+i+n+1] = (decided[n*i+i+2] + decided[n*i+i+n*2]) % 2

	ans = (ans * (2 ** min(0, is_empty-1))) % MOD

is_empty = 0
if n*(n-2)+(n-2) not in decided:
	is_empty += 1
if n*(n-2)+(n-2)+1 not in decided:
	is_empty += 1
if n*(n-2)+(n-2)+n not in decided:
	is_empty += 1
if n*(n-2)+(n-2)+n+1 not in decided:
	is_empty += 1

if is_empty == 0:
	if (decided[n*(n-2)+(n-2)] + decided[n*(n-2)+(n-2)+n+1]) % 2 != (decided[n*(n-2)+(n-2)+1] + decided[n*(n-2)+(n-2)+n]) % 2:
		ans *= 0

if is_empty == 1 and n*(n-2)+(n-2)+n+1 not in decided:
	decided[n*(n-2)+(n-2)+n+1] = (decided[n*(n-2)+(n-2)+1] + decided[n*(n-2)+(n-2)+n]) % 2 - decided[n*(n-2)+(n-2)]

ans = (ans * (2 ** max(0, is_empty-1))) % MOD


print(ans)

Fixed, accepted submission:
from collections import Counter
import sys
MOD = 998244353
n, m = map(int, input().split())
d = Counter()
ans = 1
d_num = 0
def k(a, n):
	x = 1
	while n > 0:
		if n&1 == 1:
			x = (x*a) % MOD
		a = (a**2) % MOD
		n >>= 1
	return x
for _ in range(m):
	a, b, c = map(int, input().split())
	d[(a-1)*n+b-1] = c
	if abs(a-b) >= 3:
		if (b-1)*n+a-1 not in d:
			d_num += 1
			d[(b-1)*n+a-1] = c
		elif d[(b-1)*n+a-1] != d[(a-1)*n+b-1]:
			print(0)
			sys.exit()
ans = (ans*k(2, ((n-2)*(n-3)//2)-d_num))%MOD
e = 0
if 0 not in d:
	e+=1
if 1 not in d:
	e+=1
if n not in d:
	e+=1
if n+1 not in d:
	e+=1
if e == 0 and n*0+0 in d:
	if (d[n*0+0]+d[n+1])%2!=(d[1]+d[n]) % 2:
		ans *= 0
if e == 1 and n+1 not in d and n*0+0 in d:
	d[n+1] = (d[1]+d[n])%2-d[n*0+0]
ans = (ans*(2**max(0, e-1))) % MOD
for i in range(n-2):
	e = 0
	if n*i+i+2 not in d:
		e += 1
	if n*i+i+2*n not in d:
		e += 1
	if e == 0 and n*i+i+n+1 in d:
		if (d[n*i+i+2] + d[n*i+i+n+1] + d[n*i+i+2*n]) % 2 != 0:
			ans *= 0
	if e == 0 and n*i+i+n+1 not in d:
		d[n*i+i+n+1] = (d[n*i+i+2] + d[n*i+i+n*2]) % 2
		ans = (ans*499122177) % MOD
	ans = (ans*(2 ** max(0, e-1))) % MOD
	e=0
	if n*(i+1)+i+2 not in d:
		e += 1
	if n*(i+1)+i+1+n not in d:
		e += 1
	if n*(i+1)+i+n+2 not in d:
		e += 1
	if e == 0 and n*(i+1)+i+1 in d:
		if (d[n*(i+1)+i+1]+d[n*(i+1)+i+n+2]) % 2 != (d[n*(i+1)+i+2]+d[n*(i+1)+i+1+n])%2:
			ans *= 0
	if e == 0 and n*(i+1)+i+1 not in d:
		ans = (ans*499122177) % MOD
	if e == 1 and n*(i+1)+i+2+n not in d and n*(i+1)+i+1 in d:
		d[n*(i+1)+i+2+n]=(d[n*(i+1)+i+2]+d[n*(i+1)+i+1+n])%2-d[n*(i+1)+i+1]
	ans = (ans*(2**max(0, e-1))) % MOD
print(ans)