def det(M, mod): M = [row[:] for row in M] N, sign, prev = len(M), 1, 1 for i in range(N-1): #print(*M, sep="\n") #print() if M[i][i] == 0: swapto = next((j for j in range(i+1, N) if M[j][i] != 0), None) if swapto is None: return 0 M[i], M[swapto], sign = M[swapto], M[i], -sign previnv = pow(prev, mod-2, mod) for j in range(i+1, N): for k in range(i+1, N): M[j][k] = (M[j][k]*M[i][i]%mod-M[j][i]*M[i][k]%mod)*previnv%mod prev = M[i][i] return sign*M[-1][-1]%mod T = int(input()) for _ in range(T): n, p = map(int,input().split()) a = [list(map(int,input().split())) for i in range(n)] if p > 2: print(0) continue # p = 2 x = [0] * n y = [0] * n for i in range(n): for j in range(n): if a[i][j] == -1: a[i][j] = 1 print(det(a, 2))