#include <bits/stdc++.h>
#define int long long int
using namespace std;
template<typename T,typename U> using P=pair<T,U>;
template<typename T> using V=vector<T>;
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}

template<typename T>auto&operator<<(ostream&s,const vector<T>&v){s<<"[";bool a=1;for(auto e:v){s<<(a?"":" ")<<e;a=0;}s<<"]";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const pair<T,U>&p){s<<"("<<p.first<<","<<p.second<<")";return s;}
template<typename T>auto&operator<<(ostream&s,const set<T>&st){s<<"{";bool a=1;for(auto e:st){s<<(a?"":" ")<<e;a=0;}s<<"}";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const map<T,U>&m){s<<"{";bool a=1;for(auto e:m){s<<(a?"":" ")<<e.first<<":"<<e.second;a=0;}s<<"}";return s;}
#define DUMP(x)  cerr<<#x<<" = "<<(x)<<endl;

struct edge { int to, cost; };

const int INF = 1e18;
const int MOD = 1e9+7;

template<int mod=MOD>
struct ModRing {
   int x;
   ModRing() : x(0) {}
   ModRing(int a) { x = ((a % mod) + mod) % mod; }

   bool operator==(const ModRing& a) const {
      return x == a.x;
   }
   bool operator!=(const ModRing& a) const {
      return x != a.x;
   }
   bool operator==(int a) const {
      return *this == ModRing(a);
   }
   bool operator!=(int a) const {
      return *this != ModRing(a);
   }
   ModRing& operator=(int a) {
      return *this = ModRing(a);
   }
   ModRing operator-() const {
      return ModRing((-x + mod) % mod);
   }
   ModRing& operator+=(const ModRing& a) {
      (x += a.x) %= mod;
      return *this;
   }
   ModRing& operator-=(const ModRing& a) {
      return *this += -a;
   }
   ModRing& operator*=(const ModRing& a) {
      (x *= a.x) %= mod;
      return *this;
   }
   const ModRing operator+(const ModRing& a) const {
      return ModRing(x) += a;
   }
   const ModRing operator-(const ModRing& a) const {
      return ModRing(x) -= a;
   }
   const ModRing operator*(const ModRing& a) const {
      return ModRing(x) *= a;
   }
   ModRing& operator+=(int a) {
      return *this += ModRing(a);
   }
   ModRing& operator-=(int a) {
      return *this -= ModRing(a);
   }
   ModRing& operator*=(int a) {
      return *this *= ModRing(a);
   }
   const ModRing operator+(int a) const {
      return ModRing(x) += a;
   }
   const ModRing operator-(int a) const {
      return ModRing(x) -= a;
   }
   const ModRing operator*(int a) const {
      return ModRing(x) *= a;
   }
   ModRing power(int n) const {
      ModRing tmp(x), res(1);
      while (n > 0) {
         if ((n & 1) == 1) res *= tmp;
         tmp *= tmp;
         n >>= 1;
      }
      return res;
   }
};
template<int mod>auto&operator<<(ostream&s,const ModRing<mod>&a){s<<a.x;return s;}

template<int mod=MOD>
struct FiniteField : ModRing<mod> {
   FiniteField() : ModRing<mod>() {}
   FiniteField(int a) : ModRing<mod>(a) {}
   FiniteField(const ModRing<mod>& a) : ModRing<mod>(a) {}

   FiniteField& operator=(int a) {
      return *this = ModRing<mod>::operator=(a);
   }
   FiniteField& operator+=(const FiniteField& a) {
      return *this = ModRing<mod>::operator+=(a);
   }
   FiniteField& operator-=(const FiniteField& a) {
      return *this = ModRing<mod>::operator-=(a);
   }
   FiniteField& operator*=(const FiniteField& a) {
      return *this = ModRing<mod>::operator*=(a);
   }
   FiniteField& operator+=(int a) {
      return *this = ModRing<mod>::operator+=(a);
   }
   FiniteField& operator-=(int a) {
      return *this = ModRing<mod>::operator-=(a);
   }
   FiniteField& operator*=(int a) {
      return *this = ModRing<mod>::operator*=(a);
   }

   FiniteField inverse() const {
      try { if (this->x == 0) throw "0 have no inverse"; }
      catch (const char* e) { cerr << e << endl; }
      return ModRing<mod>::power(mod-2);
   }
   FiniteField& operator/=(const FiniteField& a) {
      return *this *= a.inverse();
   }
   const FiniteField operator/(const FiniteField& a) const {
      return FiniteField(*this) /= a;
   }
   FiniteField& operator/=(int a) {
      return *this /= FiniteField(a);
   }
   const FiniteField operator/(int a) const {
      return FiniteField(*this) /= a;
   }
};

template<typename Field>
struct Matrix
{
   vector<vector<Field>> val;
   int m, n;
   Matrix(int a=0, int b=0) : m(a), n(b) {
      val.resize(m, vector<Field>(n));
   }
   Matrix(const vector<vector<Field>>& vec) { init(vec); }
   void init(const vector<vector<Field>>& vec) {
      m = vec.size(); n = vec[0].size();
      val.resize(m, vector<Field>(n));
      for (int i = 0; i < m; i++) for (int j = 0; j < n; j++)
         val[i][j] = vec[i][j];
   }
   Matrix& operator=(const Matrix& mat) {
      try { if (m != mat.m || n != mat.n) throw "Matrix can't be substituted"; }
      catch (const char* e) { cerr << e << endl; }
      for (int i = 0; i < m; i++) for (int j = 0; j < n; j++)
         val[i][j] = mat.val[i][j];
      return *this;
   }
   Field operator()(int i, int j) const {
      try { if (i < 0 || m <= i || j < 0 || n <= j) throw "Bad access"; }
      catch (const char* e) { cerr << e << endl; }
      return val[i][j];
   }
   Matrix& operator-() const {
      Matrix tmp(m, n);
      for (int i = 0; i < m; i++) for (int j = 0; j < n; j++)
         tmp.val[i][j] = -val[i][j];
      return tmp;
   }

   Matrix& operator+=(const Matrix& mat) {
      try { if (m != mat.m || n != mat.n) throw "Matrix can't be added"; }
      catch (const char* e) { cerr << e << endl; }
      for (int i = 0; i < m; i++) for (int j = 0; j < n; j++)
         val[i][j] += mat.val[i][j];
      return *this;
   }
   Matrix& operator-=(const Matrix& mat) {
      try { if (m != mat.m || n != mat.n) throw "Matrix can't be subtracted"; }
      catch (const char* e) { cerr << e << endl; }
      return *this += -mat;
   }
   Matrix operator*=(const Matrix& mat) {
      try { if (n != mat.m) throw "Matrix can't be producted"; }
      catch (const char* e) { cerr << e << endl; }
      Matrix tmp(m, mat.n);
      for (int i = 0; i < m; i++) for (int j = 0; j < mat.n; j++) for (int k = 0; k < n; k++)
         tmp.val[i][j] += val[i][k] * mat.val[k][j];
      n = mat.n;
      val.resize(m, vector<Field>(n));
      return *this = tmp;
   }
   const Matrix operator+(const Matrix& mat) const {
      return Matrix(val) += mat;
   }
   const Matrix operator-(const Matrix& mat) const {
      return Matrix(val) -= mat;
   }
   const Matrix operator*(const Matrix& mat) const {
      return Matrix(val) *= mat;
   }
   Matrix transpose() const {
      Matrix tmp(n, m);
      for (int i = 0; i < m; i++) for (int j = 0; j < n; j++)
         tmp.val[j][i] = val[i][j];
      return tmp;
   }
   int gaussElimination() {
      int swap_num = 0;
      int col = 0;
      for (int i = 0; i < m; i++) {
         int pivot;
         bool updated = false;
         while (col < n) {
            for (int j = i; j < m; j++) if (val[j][col] != 0) {
               pivot = j;
               updated = true;
               break;
            }
            if (updated) break;
            col++;
         }
         if (!updated) return swap_num;

         if (i != pivot) {
            swap(val[i], val[pivot]);
            swap_num++;
         }
         for (int j = i+1; j < m; j++) {
            Field ratio = val[j][col] / val[i][col];
            for (int k = col; k < n; k++) val[j][k] -= val[i][k] * ratio;
         }
         col++;
      }
      return swap_num;
   }
   int rank() const {
      Matrix A(val);
      A.gaussElimination();
      int j = 0;
      for (int i = 0; i < m; i++) {
         while (A.val[i][j] == 0) {
            j++; if (j == n) return i;
         }
      }
      return m;
   }
};
template<typename Field>ostream&operator<<(ostream&s,const Matrix<Field>&mat){s<<endl<<"[";for(int i=0;i<mat.m;i++){s<<"[";for(int j=0;j<mat.n;j++){s<<mat(i,j);if(j<mat.n-1)s<<" ";}s<<"]";if(i<mat.m-1)s<<endl;}s<<"]";return s;}

template<typename Field>
struct SquareMatrix : Matrix<Field>
{
   int num;
   SquareMatrix(int a=0) : Matrix<Field>(a, a) { num = a; }
   SquareMatrix(const vector<vector<Field>>& vec) : num(vec.size()), Matrix<Field>(vec) {
      try { if (vec.size() != vec[0].size()) throw "Matrix isn't square matrix"; }
      catch (const char* e) { cerr << e << endl; }
   }
   Field determinant() const {
      SquareMatrix A(*this);
      int swap_num = A.gaussElimination();
      Field det = (swap_num % 2 ? -1 : 1);
      for (int i = 0; i < num; i++) det *= A(i,i);
      return det;
   }
   SquareMatrix power(int n) const {
      SquareMatrix A(this->val);
      V<V<Field>> E(num,V<Field>(num));
      for (int i = 0; i < num; i++) E[i][i] = 1;
      SquareMatrix res(E);
      while (n > 0) {
         if ((n & 1) == 1) { res *= A; }
         A *= A;
         n >>= 1;
      }
      return res;
   }
};

signed main()
{
   int n, k; cin >> n >> k;
   using GF = FiniteField<>;
   V<V<GF>> a(n, V<GF>(n));
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
         int A; cin >> A;
         a[i][j] = A;
      }
   }

   SquareMatrix<GF> A(a);
   A = A.power(k);
   GF 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;
}

Measured memory_bytes: 384