/// __Macro's__
// #pragma GCC optimize("-O2")
#include <bits/stdc++.h>
using namespace std;
#define ll                   long long
#define II                   pair<int,int>
#define III                  pair<int, II>
#define VL                   vector<ll>
#define VI                   vector<int>
#define VII                  vector<II>
#define VIII                 vector<III>
#define VVI                  vector<vector<int>>
#define VVII                 vector<vector<II>>
#define fr                   first
#define sc                   second
#define mkpr                 make_pair
#define PQ                   priority_queue
#define pb                   push_back
#define eb                   emplace_back
#define all(v)               v.begin(), v.end()
#define LINE                 putc('\n', stdout)
#define SPACE                putc(' ' , stdout)
#define TAB                  putc('\t', stdout)
#define fillarr(arr, val)    fill((int*)arr, (int*)arr+(sizeof(arr)/sizeof(int)), val)
#define Log2(x)              (31^__builtin_clz(x))
#define inf                  2000000007 
#define MOD                  1000000007 
#define getint ReadInt()
const char IO_MODE = 3; // in-->00<--out (MSB-->LSB) | 0: Norm, 1: Fast 
inline ll ReadInt(){ll x=0,s=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')s=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';
c=getchar();}return s*x;}inline void WriteInt(ll x){char c[20];if(!x){putchar('0');return;}if(x<0)putchar('-'),x=-x;int i=0;while(x>0)
c[++i]=x%10,x/=10;while(i)putchar(c[i--]+48);}template <typename T>inline void out(T x){if(IO_MODE&1)WriteInt(x);else if(typeid(x)==typeid(int))
printf("%i", x);else printf("%lld", (ll)x);}template<typename T, typename... Args>inline void out(T x,Args...args){out(x);SPACE;out(args...);}
template <typename T>inline void in(T &x){if(IO_MODE&2) x=ReadInt();else if(typeid(x)==typeid(int))scanf("%i",&x);else if(typeid(x)==typeid(ll))
scanf("%lld",&x);}template<typename T, typename... Args>inline void in(T &x,Args&...args){in(x);in(args...);}
int aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,mm,nn,oo,pp,qq,rr,ss,tt,uu,vv,ww,xx,yy,zz;
int tc;
///
 
#define nax 100100
template<class T>
struct matrix{
  int n;
  vector<vector<T>> a;

  matrix(int _n, T val=0, bool scalar_matrix=0){
    n=_n;
    if(scalar_matrix){
      a.assign(n, vector<T>(n,0));
      for(int i=0 ; i<n ; i++) a[i][i]=val;
    }
    else{
      a.assign(n, vector<T>(n, val));
    }
  }

  vector<T>& operator [] (int idx){
    return a[idx];
  }
  matrix operator * (matrix b){
    matrix c = matrix(n, 0);
    for(int i=0 ; i<n ; i++)
      for(int k=0 ; k<n ; k++)
        for(int j=0 ; j<n ; j++)
          c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;
    return c;
  }
  vector<T> operator * (vector<T> b){
    vector<T> c = vector<T>(n, 0);
    for(int i=0 ; i<n ; i++)
      for(int j=0 ; j<n ; j++)
         c[i] = (c[i] + a[i][j] * b[j]) % MOD;
    return c;
  }
  matrix<T> operator + (matrix<T> b){
    matrix<T> c = matrix<T>(n, 0);
    for(int i=0 ; i<n ; i++)
      for(int j=0 ; j<n ; j++)
        c[i][j] =  (a[i][j] + b[i][j]) % MOD;
    return c;
  }
  matrix<T> operator - (matrix<T> b){
    matrix<T> c = matrix<T>(n, 0);
    for(int i=0 ; i<n ; i++)
      for(int j=0 ; j<n ; j++)
        c[i][j] = (a[i][j] - b[i][j] + MOD) % MOD;
    return c;
  }
  matrix<T> operator ^ (ll p){
    if(p==1) return *this;
    if(p%2)  return *this*(*this^(p-1));
    else {matrix<T> d = *this^(p/2); return d*d;}
  }
  matrix sumPows(ll p){ // return a^1 + a^2 + a^3 + ... a^p
    if(p==1) return *this;
    if(p%2)  return *this * ( sumPows(p-1) + matrix(n, 1, 1) );
    else {matrix<T> d = sumPows(p/2); return d * (d + matrix(n, 1, 1));}
  }
};

int main()
{
  int n;
  ll k;
  in(n, k);
  matrix<ll> m(n);
  for(int i=0 ; i<n ; i++)
    for(int j=0 ; j<n ; j++)
      cin>>m[i][j];

  m = m^k;
  ll ans = 0;
  for(int i=0 ; i<n ; i++)
    for(int j=0 ; j<n ; j++)
      ans = (ans + m[i][j])%MOD;

  cout << ans << endl;
}

Measured memory_bytes: 512