import sequtils,math proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "" .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': break result = 10 * result + k.ord - '0'.ord const MOD = 1000000007 template useMatrix = type Matrix = ref object w,h:int data: seq[int] proc `[]`(m:Matrix,x,y:int):int = m.data[x + y * m.w] proc `[]=`(m:var Matrix,x,y,val:int) = m.data[x + y * m.w] = val proc newMatrix(w,h:int):Matrix = new(result) result.w = w result.h = h result.data = newSeq[int](w * h) proc identityMatrix(n:int):Matrix = result = newMatrix(n,n) for i in 0..