require "matrix"
MOD = 10**9+7

#n次正方行列aのb乗を二分累乗で計算
def pow(n,a,b)
  if b == 0
    Matrix.unit(n)
  elsif b%2 == 0
    (pow(n,a,b/2)**2).collect{|v| v%MOD }
  else
    (a*(pow(n,a,b/2)**2).collect{|v| v%MOD}).collect{|v| v%MOD}
  end
end

N, K = gets.split.map(&:to_i)

a = []

N.times do |i|
  a[i] = gets.split.map(&:to_i)
end

m = Matrix.build(N, N){|r,c|
  a[r][c]
}

m = pow(N, m, K)

ans = 0
N.times do |i|
  N.times do |j|
    ans += m[i,j] 
    ans %= MOD
  end
end

puts ans
