Mod = 10**9 + 7
N,K,*A = `dd`.split.map &:to_i

def mul(mat1, mat2)
 m2 = mat2.transpose
 (0...N).each{ |i|
   r1 = mat1[i]
   row = r1.clone
   (0...N).each{ |j|
     r2 = m2[j]
     v = 0
     (0...N).each{ |k|
       v = (v + row[k] * r2[k]) % Mod
     }
     r1[j] = v
   }
 }
 mat1
end

mat = (0...N).map{ |i| (0...N).map{ |j| A[i*N+j] } }
prd = (0...N).map{ |i| (0...N).map{ |j| i == j ? 1 : 0 } }

b = 1
while b <= K
  if K & b > 0
    prd = mul(prd, mat)
  end
  b <<= 1
  mat = mul(mat, mat)
end

p prd.reduce(0){ |a,row| (a + row.reduce(:+)) % Mod }
