#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

template <typename T>
struct matrix {

  int m, n;
  vector<vector<T>> A;

  matrix(int mm, int nn) {
    m = mm;
    n = nn;
    A.clear();
    for (int i = 0; i < m; i++) {
      vector<T> v(n);
      A.push_back(v);
    }
  }

  matrix(const matrix<T>& o) {
    m = o.m;
    n = o.n;
    A.clear();
    for (int i = 0; i < m; i++) {
      vector<T> v(n);
      A.push_back(v);
      for (int j = 0; j < n; j++) {
        A[i][j] = T(o.A[i][j]);
      }
    }
  }

  void eye() {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        A[i][j] = (i == j) ? T(1) : T(0);
      }
    }
  }

  matrix<T>& operator=(const matrix<T>& o) {
    matrix(o.n, o.m);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        A[i][j] = o.A[i][j];
      }
    }
    return *this;
  }

  vector<T>& operator[](const int& x) { return A[x]; }

  matrix<T> operator+(const matrix<T>& o) const {
    assert(o.m == m and o.n == n);
    matrix<T> mat(m, n);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        mat[i][j] = A[i][j] + o.A[i][j];
      }
    }
    return mat;
  }

  matrix<T> operator-(const matrix<T>& o) const {
    assert(o.m == m and o.n == n);
    matrix<T> mat(m, n);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        mat[i][j] = A[i][j] - o.A[i][j];
      }
    }
    return mat;
  }

  matrix<T>& operator+=(const matrix<T>& o) {
    *this = *this + o;
    return *this;
  }

  matrix<T>& operator-=(const matrix<T>& o) {
    *this = *this - o;
    return *this;
  }

  matrix<T> operator*(const T& o) const {
    matrix<T> mat(m, n);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        mat[i][j] = A[i][j] * o;
      }
    }
    return mat;
  }

  matrix<T> operator*(const matrix<T>& o) const {
    assert(n == o.m);
    matrix<T> mat(m, o.n);
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < o.n; k++) {
          mat[i][k] += A[i][j] * o.A[j][k];
        }
      }
    }
    return mat;
  }

  matrix<T>& operator*=(const T& o) {
    *this = *this * o;
    return *this;
  }

  matrix<T>& operator*=(const matrix<T>& o) {
    *this = *this * o;
    return *this;
  }

  matrix<T> pow(ll t) const {
    assert(m == n);
    matrix<T> I(m, n);
    I.eye();
    matrix<T> mult(*this);
    for (ll i = 1; i <= t; i *= 2) {
      if ((i & t) > 0) {
        I *= mult;
      }
      mult *= mult;
    }
    return I;
  }

  bool operator==(const matrix<T>& o) const {
    if (m != o.m or n != o.n) {
      return false;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (A[i][j] != o.A[i][j]) {
          return false;
        }
      }
    }
    return true;
  }

  friend ostream& operator<<(ostream& out, const matrix<T>& mat) {
    for (int i = 0; i < mat.m; i++) {
      for (int j = 0; j < mat.n; j++) {
        out << mat.A[i][j] << " ";
      }
      if (i < mat.m - 1) {
        out << endl;
      }
    }
    return out;
  }
};

struct ring {

  ll x;
  static ll MOD;

  ring() { x = 0; }

  ring(ring& r) { x = r.x; }

  ring(ll xx) { x = (xx + MOD) % MOD; }
  ring operator+(const ring& o) const { return ring(x + o.x); }

  ring operator-(const ring& o) const { return ring(x - o.x + MOD); }

  ring operator*(const ring& o) const { return ring(x * o.x); }

  ring& operator+=(const ring& o) {
    x = (x + o.x) % MOD;
    return *this;
  }

  ring& operator-=(const ring& o) {
    x = (x - o.x + MOD) % MOD;
    return *this;
  }

  ring& operator*=(const ring& o) {
    x = ((x % MOD) * (o.x % MOD)) % MOD;
    return *this;
  }

  ring pow(const ll& t) const {
    ring res(1);
    ring mult(x);
    for (int i = 1; i <= t; i *= 2) {
      if ((i & t) > 0) {
        res *= mult;
      }
      mult *= mult;
    }
    return res;
  }

  ring operator/(const ring& o) const {
    ring d = (o.x == 2) ? ring((MOD + 1) / 2) : o.pow(MOD - 2);
    ll r = ((x % MOD) * (d.x % MOD)) % MOD;
    return ring(r);
  }

  ring operator/=(const ring& o) {
    ring d = (o.x == 2) ? ring((MOD + 1) / 2) : o.pow(MOD - 2);
    x = ((x % MOD) * (d.x % MOD)) % MOD;
    return *this;
  }

  operator int() const { return int(x); }

  bool operator==(const ring& r) { return x == r.x; }

  friend ostream& operator<<(ostream& out, const ring& r) {
    out << r.x;
    return out;
  }
};

ll ring::MOD = 1000000007;

namespace std {

template <>
struct hash<ring> {
  size_t operator()(const ring& r) {
    return hash<int>()(0xf4543eac ^ int(r.x));
  }
};

template <typename T>
struct hash<matrix<T>> {
  size_t operator()(const matrix<T>& mat) const {
    int sum = 0;
    for (int i = 0; i < mat.m; i++) {
      for (int j = 0; j < mat.n; j++) {
        sum += hash<T>()(mat.A[i][j]) ^ (sum << 4) ^ (sum >> 2);
      }
    }
    return sum;
  }
};

} // namespace std

int main() {
  int n;
  ll k;
  cin >> n >> k;
  matrix<ring> m(n, n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> m.A[i][j].x;
    }
  }
  matrix<ring> p = m.pow(k);
  ring res;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      res += p.A[i][j].x;
    }
  }
  cout << res.x << '\n';
  return 0;
}


Measured memory_bytes: 384