#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1145141919810893364
//#define INF 810114514
#define PI 3.141592653589
typedef long long ll;
typedef pair<ll,ll> PP;
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE do { cout << "0" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug false

class matrix{
    public:
    int col,row;
    vector<vector<int>> value;
    
    matrix(int Col,int Row){
        col = Col;
        row = Row;
        value.resize(Col);
        for(int i=0;i<Col;i++){
            value[i].resize(Row);
            for(int j=0;j<Row;j++){
                value[i][j]=0;
            }
        }
    }
    
	vector<int>& operator [] (int worm){
		return value[worm];
	}
    
    matrix operator+(matrix X){
        matrix R(this->col,this->row);
        if(this->col!=X.col || this->row!=X.row){
            return R;
        }
        for(int i=0;i<(this->col);i++){
            for(int j=0;j<(this->row);j++){
                R[i][j]=(value[i][j]+X[i][j]);
            }
        }
        return R;
    }
    
    matrix operator-(matrix X){
        matrix R(this->col,this->row);
        if(this->col!=X.col || this->row!=X.row){
            return R;
        }
        for(int i=0;i<(this->col);i++){
            for(int j=0;j<(this->row);j++){
                R[i][j]=(value[i][j]-X[i][j]);
            }
        }
        return R;
    }
    
    matrix operator*(int X){
        matrix R(this->col,this->row);
        for(int i=0;i<(this->col);i++){
            for(int j=0;j<(this->row);j++){
                R[i][j]=(value[i][j]*X);
                R[i][j]%=MOD;
            }
        }
        return R;
    }
    
    matrix operator/(int X){
        matrix R(this->col,this->row);
        for(int i=0;i<(this->col);i++){
            for(int j=0;j<(this->row);j++){
                R[i][j]=(value[i][j]/X);
            }
        }
        return R;
    }
    
    matrix operator%(int mod){
        matrix R(this->col,this->row);
        for(int i=0;i<(this->col);i++){
            for(int j=0;j<(this->row);j++){
                R[i][j]=(value[i][j]%mod);
            }
        }
        return R;
    }
    
    
    matrix operator*(matrix X){
        matrix R(this->col,X.row);
        if(this->row != X.col){
            return R;
        }
        for(int i=0;i<this->col;i++){
            for(int j=0;j<X.row;j++){
                for(int k=0;k<this->row;k++){
                    R[i][j]+=value[i][k]*X[k][j];
                    R[i][j]%=MOD;
                }
            }
        }
        return R;
    }
    
    friend ostream& operator<<(ostream& os, const matrix& out);
};


ostream& operator<<(ostream& os, const matrix& out){
    for(int i=0;i<out.col;i++){
        for(int j=0;j<out.row;j++){
            os << out.value[i][j] << (j==out.row-1?"\n":" ");
        }
    }
    return os;
}


//累乗(繰り返し二乗法)
matrix power(matrix b,long long e){
  matrix r(b.col,b.row);
  for(int i=0;i<b.col;i++){
      r[i][i]=1;
  }
  while(e){
      if(e&1){
          r=(r*b);
      }
      b=(b*b);
      e >>=1;
      r = r%MOD;
      b = b%MOD;
  }
  return r;
}

signed main(void){
    
    int N,K,a;
    cin >> N >> K;
    matrix A(N,N),R(N,N);
    REP(i,N){
        REP(j,N){
            cin >> a;
            A[i][j]=a;
        }
    }
    
    
    #if debug
    cout << A << endl;
    R = power(A,1);
    cout << R << endl;
    #endif
    
    R = power(A,K);
    
    #if debug
    cout << R << endl;
    #endif
    
    int Ans=0;
    REP(i,N){
        REP(j,N){
            Ans+=R[i][j];
            Ans%=MOD;
        }
    }
    cout << Ans << endl;
    return 0;
}



Measured memory_bytes: 384