#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FOR(i,a,b) for (int i=(a),for_##i##_max=(b);i<=(for_##i##_max);++i)
#define RFOR(i,a,b) for (int i=(a),rfor_##i##_min=(b);i>=(rfor_##i##_min);--i)
#define REP(i,n) for (int i=0,rep_##i##_len=(n);i<(rep_##i##_len);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

namespace { // Basics

template<typename T>
void chmin(T &x, T y) {
  x = std::min(x, y);
}

template<typename T>
void chmax(T &x, T y) {
  x = std::max(x, y);
}

template<typename T>
__attribute__((unused))
const auto minimum = static_cast<T const &(*)(T const &, T const &)>(std::min);

template<typename T>
__attribute__((unused))
const auto maximum = static_cast<T const &(*)(T const &, T const &)>(std::max);

template<typename T>
int cmp3(T lhs, T rhs) { return (lhs == rhs) ? 0 : (lhs > rhs) ? 1 : -1; }

template<typename T>
__attribute__((unused))
T constexpr infty = std::numeric_limits<T>::max / 3;
template<>
__attribute__((unused))
auto constexpr infty<int> = 1'100'100'100;
template<>
__attribute__((unused))
auto constexpr infty<long long int> = 100'500'400'300'200'100LL;

using lli = long long int;
using ld = long double;

} // namespace
namespace  { // Matrix

template<typename T>
class vec {
  public:
    using type = std::vector<T>;

    explicit vec(size_t s): size_(s), p(std::make_unique<type>(s)) {}
    explicit vec(type const &v): size_(v.size()), p(std::make_unique<type>(v)) {}
    vec(vec const &v) = delete;
    vec &operator=(vec const &) = delete;
    vec(vec &&v) noexcept = default;
    vec &operator=(vec &&v) noexcept = default;
    ~vec() = default;

    vec clone() const {
      auto v = static_cast<vec>(*p);
      return v;
    }
    size_t const size() const {
      return size_;
    }
    T &operator[](size_t i) {
      return (*p)[i];
    }
    T const &operator[](size_t i) const {
      return (*p)[i];
    }
    T const at(size_t i) const {
      return (*p)[i];
    }
    void set(size_t i, T x) {
      (*p)[i] = x;
    }
  private:
    size_t size_;
    std::unique_ptr<type> p;
};

template <typename T>
class matrix {
  public:
    using vec_type = std::vector<T>;
    using type = std::vector<vec_type>;

    explicit matrix(size_t h, size_t w):
      width_(w), height_(h), p(std::make_unique<type>(h, vec_type(w))) {}
    explicit matrix(size_t n): matrix(n, n) {}
    explicit matrix(type const &m):
      width_(m[0].size()), height_(m.size()), p(std::make_unique<type>(m)) {}
    matrix(matrix const &m) = delete;
    matrix &operator=(matrix const &m) = delete;
    matrix(matrix &&m) noexcept = default;
    matrix &operator=(matrix &&m) noexcept = default;
    ~matrix() = default;

    matrix clone() const {
      auto m = static_cast<matrix>(*p);
      return m;
    }
    size_t const width() const {
      return width_;
    }
    size_t const height() const {
      return height_;
    }
    T const at(size_t i, size_t j) const {
      return (*p)[i][j];
    }
    vec_type &operator[](size_t i) {
      return (*p)[i];
    }
    vec_type const &operator[](size_t i) const {
      return (*p)[i];
    }
    void set(size_t i, size_t j, T x) {
      (*p)[i][j] = x;
    }
    static matrix identity(size_t n) {
      matrix A(n);
      for (int i = 0; i < n; ++i) {
        A.set(i, i, 1);
      }
      return A;
    }
  private:
    size_t width_;
    size_t height_;
    std::unique_ptr<type> p;
};

template<typename T>
vec<T> operator*(matrix<T> const &A, vec<T> const &x) {
  vec<T> y(A.height());
  for (size_t i = 0, h = A.height(); i < h; ++i)
    for (size_t j = 0, w = A.width(); j < w; ++j)
      y[i] += A[i][j] * x[j];
  return y;
}

template<typename T>
matrix<T> operator*(matrix<T> const &A, matrix<T> const &B) {
  assert(A.width() == B.height());
  matrix<T> C(A.height(), B.width());
  for (size_t i = 0, h = A.height(); i < h; ++i)
    for (size_t k = 0, r = A.width(); k < r; ++k)
      for (size_t j = 0, w = B.width(); j < w; ++j)
        C[i][j] += A[i][k] * B[k][j];
  return C;
}

template<typename T>
matrix<T> pow(matrix<T> const &A, lli n) {
  auto M = A.clone();
  auto ans = matrix<T>::identity(M.height());
  while (n > 0) {
    if (n & 1) {
      ans = ans * M;
    }
    M = M * M;
    n >>= 1;
  }
  return ans;
}

} // namespace
namespace  { // Mod

template<int mod>
class modulo {
  public:
    constexpr modulo(): num(0) {}
    constexpr modulo(int n): num(n >= 0 ? n%mod : mod-((-n)%mod)) {}
    constexpr modulo(modulo const &) = default;
    constexpr int data() const { return num; }
    constexpr modulo &operator+=(modulo other) {
      num = (num + other.num) % mod;
      return *this;
    }
    constexpr modulo &operator-=(modulo other) {
      num = (num + mod - other.num) % mod;
      return *this;
    }
    constexpr modulo &operator*=(modulo other) {
      long long n = ((long long)num) * ((long long)other.num);
      num = n % mod;
      return *this;
    }
    constexpr modulo &operator/=(modulo other) {
      *this *= other.inverse();
      return *this;
    }
    constexpr static modulo pow(modulo a, int n, modulo res) {
      if (n == 0) { return res; }
      return pow(a*a, n/2, (n&1)?(res*a):res);
    }
    constexpr modulo inverse() const {
      return pow(*this, mod-2, modulo(1));
    }
  private:
    int num;
};

template<int mod>
constexpr modulo<mod> const operator+(modulo<mod> a, modulo<mod> b) {
  a += b;
  return a;
}

template<int mod>
constexpr modulo<mod> const operator-(modulo<mod> a, modulo<mod> b) {
  a -= b;
  return a;
}

template<int mod>
constexpr modulo<mod> const operator*(modulo<mod> a, modulo<mod> b) {
  a *= b;
  return a;
}

template<int mod>
constexpr modulo<mod> const pow(modulo<mod> a, int n) {
  return modulo<mod>::pow(a, n, 1);
}

template<int mod>
constexpr modulo<mod> const operator/(modulo<mod> a, modulo<mod> b) {
  a /= b;
  return a;
}

template<int mod, int size>
struct factorial {
  using Mod = modulo<mod>;
  Mod fact[size];
  Mod invfact[size];
  constexpr factorial(): fact(), invfact() {
    fact[0] = 1;
    for (int i = 1; i < size; ++i) {
      fact[i] = Mod(i) * fact[i-1];
    }
    invfact[size-1] = fact[size-1].inverse();
    for (int i = size - 1; i > 0; --i) {
      invfact[i-1] = invfact[i] * Mod(i);
    }
  }
  Mod const comb(int n, int k) const {
    assert(k <= n && n < size);
    return fact[n] * invfact[k] * invfact[n-k];
  }
};

} // namespace

using namespace std;

using Mod = modulo<1'000'000'007>;

void solve(lli N, lli K, matrix<Mod> const &M) {
  auto const A = pow(M, K);
  Mod ans = 0;
  REP(r, N) REP(c, N) {
    ans += A.at(r, c);
  }
  cout << ans.data() << endl;
}

int main() {
  lli N, K; cin >> N >> K;

  matrix<Mod> M(N, N);
  REP(r, N) REP(c, N) {
    int A; cin >> A;
    M[r][c] = A;
  }
  solve(N, K, M);
  return 0;
}


Measured memory_bytes: 256