#include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

#ifdef __LOCAL
    #include <filesystem>
    namespace fs = std::filesystem;
#endif

using namespace std;
using ll = long long int;
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };

template< ll mod >
struct ModInt {
  ll x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
ll const mod = 1e9+7;
using modint = ModInt< mod >;
using matrix = vector<vector<modint> >;


int N;
ll K;
matrix M;
void input(){
  fast_io();
  #ifdef __LOCAL
      fs::path p = __FILE__;
      fs::path input,output;
      input = output = p.parent_path();
      input += string("/input/") + string(p.stem()) + string(".txt");
      output += string("/output/") + string(p.stem()) + string(".txt");
      freopen(input.c_str(), "r", stdin);
      freopen(output.c_str(), "w", stdout);
  #endif
  cin >> N >> K;
  M.assign(N,vector<modint>(N,0));
  for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> M[i][j];
}

matrix matTimes(matrix a, matrix b){
  int n=a.size();
  matrix ret(n,vector<modint>(n,0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        ret[i][j] += a[i][k]*b[k][j];
  return ret;
}


matrix pow_mat(matrix m, ll n){
  matrix ret(m.size(),vector<modint>(m.size(),0));
  for (int i = 0; i < m.size(); i++) ret[i][i] = 1;
  while(n>0){
    if(n&1) ret=matTimes(ret,m);
    m = matTimes(m,m);
    n>>=1;
  }
  return ret;
}

int solve(){
  auto A = pow_mat(M,K);
  modint ans=0;
  for (int i = 0; i < N; i++)
  {
    for (int j = 0; j < N; j++)
    {
      ans += A[i][j];
    }
    
  }
  cout << ans << endl;
  return 0;
}

int main()
{
  // maketestcase(400);
  input();
  solve();
  return 0;
}


Measured memory_bytes: 384