#include <bits/stdc++.h>
const int default_mod = 1000000007;
template<typename T>
class Matrix {
    int sz;
    int mod;
public:
    std::vector<std::vector<T>> A;
    int size() {
        return sz;
    }
    Matrix() {}
    Matrix(std::vector<std::vector<T>> v, int m = default_mod) {
        mod = m;
        sz = v.size();
        assert(sz > 0);
        assert(sz == (int)(v[0].size()));
        A.clear();
        A.resize(sz, std::vector<T>(sz, 0));
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                A[i][j] = v[i][j];
            }
        }
    }
    Matrix(Matrix<T> &B, int m = default_mod) {
        mod = m;
        sz = B.sz;
        A.clear();
        A.resize(sz, std::vector<T>(sz));
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                A[i][j] = B[i][j];
            }
        }
    }
    Matrix(int n, int m = default_mod) {
        mod = m;
        sz = n;
        A.clear();
        A.resize(sz, std::vector<T>(sz, 0));
    }
    void to_I() {
        A.clear();
        A.resize(sz, std::vector<T>(sz, 0));
        for(int i=0;i<sz;++i) {
            A[i][i] = 1;
        }
    }
    void print() {
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                std::cout << A[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
    std::vector<T>&operator[](int i) {
        return A[i];
    }
    Matrix<T> &operator+=(Matrix<T> B) {
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                A[i][j] += B[i][j];
                A[i][j] %= mod;
            }
        }
        return (*this);
    }
    Matrix<T> &operator*=(Matrix<T> B) {
        std::vector<std::vector<T>> C(sz, std::vector<T>(sz, 0));
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                for(int k=0;k<sz;++k) {
                    C[i][j] += (A[i][k] * B[k][j]) % mod;
                    C[i][j] %= mod;
                }
            }
        }
        A = C;
        return (*this);
    }
    Matrix<T> operator *(Matrix<T> B) {
        Matrix<T> C(sz, mod);
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                for(int k=0;k<sz;++k) {
                    C[i][j] += (A[i][k] * B[k][j]) % mod;
                    C[i][j] %= mod;
                }
            }
        }
        return C;
    }
    std::vector<T> operator *(std::vector<T> v) {
        assert(sz = (int)(v.size()));
        std::vector<T> res(sz, 0);
        for(int i=0;i<sz;++i) {
            for(int j=0;j<sz;++j) {
                res[i] += (A[i][j] * v[j]) % mod;
                res[i] %= mod;
            }
        }
        return res;
    }
    Matrix<T> &operator^=(long long k) {
        Matrix<T> B(sz, mod);
        B.to_I();
        while(k > 0) {
            if(k&1) B *= *this;
            *this *= *this;
            k = k >> (1ll);
        }
        A = B.A;
        return (*this);
    }
};
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n; cin >> n;
  ll k; cin >> k;
  vvll a(n, vll(n));
  for(int i=0;i<n;++i) {
    for(int j=0;j<n;++j) {
      cin >> a[i][j];
    }
  }
  Matrix<ll> A(a);
  A ^= k;
  ll ans = 0;
  for(int i=0;i<n;++i) {
    for(int j=0;j<n;++j) {
      ans += A[i][j];
      ans %= MOD;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 384