#include <bits/stdc++.h>
using namespace std;
using Mat = vector<vector<int>>;
const int mod = 1000000007;
Mat mul(Mat a, Mat b){
  int n = a.size();
  Mat r = vector<vector<int>>(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      for(int k = 0; k < n; k++){
        r[i][j] += 1ll * a[i][k] * b[k][j] % mod;
        if(r[i][j] >= mod) r[i][j] -= mod;
      }
    }
  }
  return r;
}
int main(){
  int n; long long k; cin >> n >> k;
  Mat a = vector<vector<int>>(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) cin >> a[i][j];
  Mat b = vector<vector<int>>(n, vector<int>(n, 0));
  for(int i = 0; i < n; i++) b[i][i] = 1;
  for(; k ; k >>= 1, a = mul(a, a)) if(k&1) b = mul(b, a);
  int ans = 0;
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {
    ans += b[i][j];
    if(ans >= mod) ans -= mod;
  }
  cout << ans << endl;
  return 0;
}
        
