#include<bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline    cerr << __LINE__ << endl;

using ll = long long;
template<class T, class U> using P = pair<T,U>;
template<class T> using Heap = priority_queue<T>;
template<class T> using heaP = priority_queue<T,vector<T>,greater<T>>;
template<class T,class U> using umap = unordered_map<T,U>;
template<class T> using uset = unordered_set<T>;

template<class T>
bool ChangeMax(T&a,const T&b){ return a < b && (a = b || true); }

template<class T>
bool ChangeMin(T&a,const T&b){ return a > b && (a = b || true); }

template<class T, size_t N, class U>
void Fill(T (&a)[N], const U&v){ fill((U*)a,(U*)(a+N),v); }

template<class T>
istream& operator >> (istream&is, vector<T>&v){ for(auto&e:v)is >> e; return is; }

template<class T>
class Matrix{
  unsigned row_,col_;
  vector<vector<T>> matrix_;
public:
  Matrix():row_(0),col_(0),matrix_(vector<vector<T>>(0)){}
  Matrix(unsigned r,unsigned c)
    :row_(r),col_(c),matrix_(vector<vector<T>>(r,vector<T>(c))){}
  Matrix(const vector<vector<T>>&m)
    :row_(m.size()),col_(m.front().size()),matrix_(m){}
  Matrix(unsigned r,unsigned c,const T&v)
    :row_(r),col_(c),matrix_(vector<vector<T>>(r,vector<T>(c,v))){}  
  vector<T>& operator[](unsigned index){
    return matrix_[index];
  }
  const vector<T>& operator[](unsigned index)const{
    return matrix_[index];
  }
  unsigned row()const{return row_;}
  unsigned col()const{return col_;}
  unsigned size()const{return row_*col_;}
  Matrix<T> operator+(const Matrix<T>&mt)const{
    Matrix m(row_,col_,0);
    for(unsigned i = 0; i < row_; ++i){
      for(unsigned j = 0; j < col_; ++j){
	m[i][j] = (*this)[i][j] + mt[i][j];
      }
    }
    return m;
  }
  Matrix<T> operator-(const Matrix<T>&mt)const{
    Matrix m(row_,col_,0);
    for(unsigned i = 0; i < row_; ++i){
      for(unsigned j = 0; j < col_; ++j){
	m[i][j] = (*this)[i][j] - mt[i][j];
      }
    }
    return m;
  }
  Matrix<T> operator*(const Matrix<T>&mt)const{
    Matrix m(row_,mt.col(),0);
    for(unsigned i = 0; i < m.row(); ++i){
      for(unsigned j = 0; j < m.col(); ++j){
	for(unsigned k = 0; k < mt.row(); ++k){
	  m[i][j] += (*this)[i][k]*mt[k][j];
	}
      }
    }
    return m;
  }
  Matrix<T> Pow(ll k){
    if(k == 0) {
      Matrix<T> mt(this->row(),this->col(),0);
      for(unsigned i = 0; i < this->row(); ++i)
	mt[i][i] = 1;
      return mt;
    }
    if(k & 1) return (*this) * (this->Pow(k-1));
    Matrix<T> mt(this->Pow(k>>1));
    return mt*mt;
  }
};


template<long long mod>
class modint{
private:
  long long num;
  long long extgcd(long long a, long long b, long long&x, long long&y)const {
    long long d = a;
    if(b != 0){d = extgcd(b, a%b, y, x);  y -= (a/b) * x;}
    else{x = 1; y = 0;}
    return d;
  }
  long long modinverse()const{
    long long x, y;
    extgcd(num, mod, x, y);
    return (mod + x%mod)%mod;
  }
public:
  modint():num(0){}
  modint(const long long x):num((x < 0?(x+mod):x)%mod ){}
  friend modint operator+(const modint&a, const modint&b)
  {    return modint(a.num + b.num);  }
  friend modint operator-(const modint&a, const modint&b)
  {    return modint(a.num - b.num);  }
  friend modint operator*(const modint&a, const modint&b)
  {    return modint(a.num * b.num);  }
  friend modint operator/(const modint&a, const modint&b)
  {    return modint(a*b.modinverse());  }
  modint& operator+=(const modint&b)
  {    return (*this) = (*this) + b;  }
  modint& operator-=(const modint&b)
  {    return (*this) = (*this) - b;  }
  modint& operator*=(const modint&b)
  {    return (*this) = (*this) * b;  }
  modint& operator/=(const modint&b)
  {    return (*this) = (*this) / b;  }
  modint& operator++()
  {    (*this)+=1; return (*this);  }
  modint& operator--()
  {    (*this)-=1; return (*this);  }
  modint operator++(int){
    modint tmp(*this);
    ++(*this);
    return tmp;
  }
  modint operator--(int){
    modint tmp(*this);
    --(*this);
    return tmp;
  }    
  modint pow(unsigned long long i){
    if(i == 0) return 1;
    if(i & 1){
      return (*this) * (this->pow(i-1));
    }
    modint<mod>hpow = (this->pow(i>>1));
    return hpow * hpow;
  }
  friend ostream& operator<<(ostream&os, const modint&a){
    os << a.num;
    return os;
  }
  friend istream& operator>>(istream&is, modint&a){
    long long x; is >> x;
    a = x;
    return is;
  }
};

using mint = modint<(ll)(1e+9)+7>;

int main(){
  ll n, k; cin >> n >> k;
  vector<vector<mint>> v(n,vector<mint>(n));
  cin >> v;
  Matrix<mint> matrix(Matrix<mint>(v).Pow(k));
  mint ans = 0;
  for(unsigned i = 0; i < matrix.row(); ++i)
    for(unsigned j = 0; j < matrix.col(); ++j)
      ans += matrix[i][j];
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 512