#include <bits/stdc++.h>
using namespace std;
#define SZ(v) int((v).size())
#define ALL(vec) begin(vec), end(vec)
typedef long long i64;
template<typename T> inline bool uax(T &x, T y) {return (y > x) ? x = y, true : false;}
template<typename T> inline bool uin(T &x, T y) {return (y < x) ? x = y, true : false;}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
  stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
string to_string(char c) { return "'" + string(1, c) + "'";}
string to_string(string s) { return '"' + s + '"';}
string to_string(const char* s) { return to_string((string) s);}
template<typename A> string to_string(A);
template<typename A, typename B> string to_string(pair<A, B> p){
  return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";}
template<typename A> string to_string(A v) {bool f = false; string r = "{"; 
  for (auto x: v) {if (f) r += ", "; r += to_string(x); f = true;} return r += "}";}
template<typename A> string to_string(vector<vector<A>> v) {string r; 
  for (auto x: v) r += "\n" + to_string(x); return r;}
int Nerr;
template<typename A> string to_string(A *p) {return to_string(vector<A>(p, p + Nerr));}
void err(istream_iterator<string>) { cerr << endl; }
template<typename T,typename... Args> void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " =: " << to_string(a) << "; "; err(++it, args...); }
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
int const MOD = 1e9 + 7;
long long const INF = 1e18 + 42;
/***********************************************************************/


struct Matrix {
  static const int MOD = 1e9 + 7;
  static const long long SQMOD = (long long) MOD * MOD;
  static int const N = 5;
  using T = int;
  T x[N][N];
  int n, m;
  Matrix(int _n = N, int _m = N, T val = 0): n(_n), m(_m) {
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
      x[i][j] = val;
  }
  Matrix(const vector<vector<int>> &&mat) {
    n = (int) mat.size();
    m = (int) mat[0].size();
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
      x[i][j] = mat[i][j];
  }
  Matrix& operator = (const vector<vector<int>> &&mat) {
    return *this = Matrix(forward<decltype(mat)>(mat));
  }
  T* operator [] (int r) {
    return x[r];
  }
  const T* operator [] (int r) const {
    return x[r];
  }
  static Matrix unit(int n) {
    Matrix res(n, n);
    for (int i = 0; i < n; i++) res[i][i] = 1;
    return res;
  }
  Matrix& operator += (const Matrix &rhs) {
    assert (n == rhs.n && m == rhs.m);
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
      x[i][j] += rhs[i][j];
      if (x[i][j] >= MOD) x[i][j] -= MOD;
    }
    return *this;
  }
  Matrix operator + (const Matrix &rhs) const {
    Matrix lhs(*this);
    return lhs += rhs;
  }
  friend Matrix operator * (const Matrix &A, const Matrix &B) {
    assert (A.m == B.n);
    Matrix res(A.n, B.m);
    for (int i = 0; i < res.n; i++) for (int j = 0; j < res.m; j++) {
      long long sum = 0LL;
      for (int k = 0; k < A.m; k++) {
        sum += (long long) A[i][k] * B[k][j];
        if (sum >= SQMOD) sum -= SQMOD;
      }
      res[i][j] = (int) (sum % MOD);
    }
    return res;
  }
  friend Matrix operator ^ (Matrix base, long long ex) {
    assert (base.n == base.m);
    Matrix res = Matrix::unit(base.n);
    while (ex > 0) {
      if (ex & 1) res = res * base;
      base = base * base;
      ex >>= 1;
    }
    return res;
  }
  Matrix& operator *= (const Matrix &rhs) {
    return *this = *this * rhs;
  }
  Matrix& operator ^= (const long long &ex) {
    return *this = *this ^ ex;
  }
  friend ostream &operator << (ostream &os, const Matrix& A) {
    os << "(" << A.n << " x " << A.m << ")" << endl;
    os << string(A.m * 2, '-');
    for (int i = 0; i < A.n; ++i) {
      os << endl;
      for (int j = 0; j < A.m; ++j) os << A[i][j] << ' ';
    }
    return os << endl << string(A.m * 2, '-');
  }
};

// Calculate geometric series: A^0 + A^1 + ... + A^k
/*****************************************************/
vector<vector<int>> matmul(const vector<vector<int>> &a, const vector<vector<int>> &b, int mod = MOD) {
    int r = a.size(), c = b[0].size(), m = a[0].size();
    assert(m == (int) b.size());
    vector<vector<int>> res(r, vector<int>(c, 0LL));
    for (int i = 0; i < r; ++i)
        for (int j = 0; j < c; ++j)
            for (int k = 0; k < m; ++k) {
                res[i][j] += (long long) a[i][k] * b[k][j] % MOD;
                if (res[i][j] >= MOD) res[i][j] -= MOD;
            }
    return res;
};
vector<int> vecmul(const vector<int> &a, const vector<vector<int>> &b, int mod = MOD) {
    // vector {.., .., ..} x {{..}, {..}, .., {..}}
    // return vector
    int m = a.size();
    assert(m == (int) b.size() && b.size() == b[0].size());
    vector<int> res(m, 0);
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < m; ++j) {
            res[i] += (long long) a[j] * b[j][i] % MOD;
            if (res[i] >= MOD) res[i] -= MOD;
        }
    }
    return res;
};
vector<vector<int>> matpow(vector<vector<int>> base, long long ex) {
    assert(base[0].size() == base.size());
    int s = base.size();
    vector<vector<int>> res(s, vector<int>(s, 0));
    for (int i = 0; i < s; ++i) res[i][i] = 1;
    while (ex > 0) {
        if (ex & 1) res = matmul(base, res);
        base = matmul(base, base);
        ex >>= 1;
    }
    return res;
};


/**************************************/

void solve_case() {
  long long n, k;
  cin >> n >> k;
  vector<vector<int>> vec(n, vector<int> (n)), ans;
  for(auto &a : vec)for(auto &b : a)cin >>b;
  ans = matpow(vec, k);
  long long answer = 0;
  for(auto &a : ans)for(auto b : a)answer += b;
  cout << answer % MOD << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
 
  int tt = 1;
  //~ cin >> tt;
  while (tt--) {
    solve_case();
  } 
}


Measured memory_bytes: 256