#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ALL(a) begin(a), end(a)
#define SZ(a) ((int)(a).size())

#define REP(a, b) for (int a = 0; a < (b); a++)
#define FOR(a, b, c) for (int a = (b); a <= (c); a++)
#define REPD(a, b) for (int a = (b) - 1; a >= 0; a--)
#define FORD(a, b, c) for (int a = (b); a >= (c); a--)

#define MIN(a, b) (a) = min((a), (b))
#define MAX(a, b) (a) = max((a), (b))

#ifdef __DEBUG
#define debug if (true)
#else
#define debug if (false)
#endif

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T> using V = vector<T>;
template <typename T> using VV = vector<V<T>>;
template <typename T> using VVV = vector<VV<T>>;

template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2> &p) {
  return is >> p.fi >> p.se;
}

// {{{ ModInt<MOD>
// ax + by = gcd(a, b)
template <typename T>
T ext_gcd(T a, T b, T &x, T &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T xx, yy, g = ext_gcd(b, a % b, xx, yy);
  x = yy;
  y = xx - yy * (a / b);
  return g;
}

template<int MOD>
struct ModInt {
 public:
  using sum_type = int;
  using prod_type = long long;

  static_assert(MOD > 0, "MOD should be positive");
  static_assert(MOD <= numeric_limits<sum_type>::max() - MOD, "Plus should not overflow");
  static_assert(MOD <= numeric_limits<prod_type>::max() / MOD, "Multiply should not overflow");

  struct good_val {};

  ModInt() : val_(0) {}

  ModInt(int val) : val_(val % MOD) { fix(op_neg{}); }

  ModInt(ll val) : val_(val % MOD) { fix(op_neg{}); }

  ModInt(int val, good_val) : val_(val) {
#ifdef __DEBUG
    assert(val_ >= 0 && val_ < MOD);
#endif
  }

  ModInt& operator+=(const ModInt &o) { val_ += o.val_; fix(op_pos{}); return *this; }
  ModInt& operator-=(const ModInt &o) { val_ -= o.val_; fix(op_neg{}); return *this; }
  ModInt& operator*=(const ModInt &o) { val_ = static_cast<prod_type>(1) * val_ * o.val_ % MOD; return *this; }
  ModInt& operator/=(const ModInt &o) { return operator*=(o.inverse()); }

  friend ModInt operator+(ModInt a, const ModInt &b) { a += b; return a; }
  friend ModInt operator-(ModInt a, const ModInt &b) { a -= b; return a; }
  friend ModInt operator*(ModInt a, const ModInt &b) { a *= b; return a; }
  friend ModInt operator/(ModInt a, const ModInt &b) { a /= b; return a; }

  ModInt inverse() const {
    prod_type a, b, g = ext_gcd<prod_type>(val_, MOD, a, b);
    if (g != 1) {
      throw;
    }
    debug assert(((*this) * a).val_ == 1);
    return ModInt(a);
  }

  sum_type get() const { return val_; }

 private:
  sum_type val_;
  struct op_neg {};
  struct op_pos {};

  void fix(op_neg) { if (val_ < 0) val_ += MOD; }
  void fix(op_pos) { if (val_ >= MOD) val_ -= MOD; }
};
// }}}

const int MOD = 1e9 + 7;

using MI = ModInt<MOD>;

VV<MI> matMul(const VV<MI> &a, const VV<MI> &b) {
  int n = SZ(a);
  VV<MI> ret(n, V<MI>(n));
  REP(i, n) REP(j, n) REP(k, n) ret[i][j] += a[i][k] * b[k][j];
  return ret;
}

VV<MI> power(VV<MI> base, ll p) {
  int n = SZ(base);
  VV<MI> ret(n, V<MI>(n));
  REP(i, n) REP(j, n) ret[i][j] = i == j ? 1 : 0;
  while (p > 0) {
    if (p & 1) ret = matMul(ret, base);
    base = matMul(base, base);
    p >>= 1;
  }
  return ret;
}

void PRINT(const VV<MI> &a) {
  int n = SZ(a);
  REP(i, n) {
    REP(j, n) printf("%d ", a[i][j].get());
    puts("");
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  ll k;
  cin >> n >> k;
  VV<MI> mat(n, V<MI>(n));
  REP(i, n) REP(j, n) {
    int x;
    cin >> x;
    mat[i][j] = x;
  }
  debug PRINT(mat);
  mat = power(mat, k);
  MI ans = 0;
  REP(i, n) REP(j, n) ans += mat[i][j];
  cout << ans.get() << endl;
}


Measured memory_bytes: 384