/*
 *    author:  Gary Shih
 *    created: 2020-05-06 11:09:31
 */
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

// Mod Integer {{{
// {{{
template <int _MOD>
class ModInt {
 public:
  static const auto MOD = _MOD;
  template <class T>
  using integral_only = typename enable_if<is_integral<T>::value>::type;

  int x;
  constexpr ModInt() : x() {}
  template <class T, integral_only<T>* = nullptr>
  ModInt(T _x) {
    x = _x % MOD;
    if (x < 0) x += MOD;
  }
  ModInt operator-() const { return {x == 0 ? 0 : MOD - x}; }
  ModInt& operator+=(ModInt rhs) {
    x += rhs.x;
    if (x >= MOD) x -= MOD;
    return *this;
  }
  ModInt& operator-=(ModInt rhs) {
    x -= rhs.x;
    if (x < 0) x += MOD;
    return *this;
  }
  ModInt& operator*=(ModInt rhs) {
    x = (long long)x * rhs.x % MOD;
    return *this;
  }
  ModInt& operator/=(ModInt rhs) { return *this *= rhs.inv(); }
  ModInt operator+(ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(ModInt rhs) const { return ModInt(*this) /= rhs; }
  ModInt inv() const {
    // should work for non-prime MOD if gcd(x, MOD) = 1
    int a = x, b = MOD, u = 1, v = 0;
    while (b != 0) {
      int t = a / b;
      a -= t * b, u -= t * v;
      swap(a, b), swap(u, v);
    }
    return u;
  }
  template <class T, integral_only<T>* = nullptr>
  ModInt pow(T e) {
    ModInt r = 1, p = *this;
    while (e) {
      if (e & 1) r *= p;
      p *= p, e >>= 1;
    }
    return r;
  }
  bool operator==(ModInt rhs) const { return x == rhs.x; }
  bool operator!=(ModInt rhs) const { return x != rhs.x; }
  bool operator<(ModInt rhs) const { return x < rhs.x; }
  bool operator<=(ModInt rhs) const { return x <= rhs.x; }
  bool operator>(ModInt rhs) const { return x > rhs.x; }
  bool operator>=(ModInt rhs) const { return x >= rhs.x; }
  friend string to_string(ModInt i) { return to_string(i.x); }
  friend ostream& operator<<(ostream& os, ModInt o) { return os << o.x; }
  friend istream& operator>>(istream& is, ModInt& i) {
    long long foo;
    is >> foo;
    foo %= MOD;
    if (foo < 0) foo += MOD;
    i.x = foo;
    return is;
  }
};
// }}}
const int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
// }}}
// Debug {{{
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) { return '"' + s + '"'; }

string to_string(const char* s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(char c) { return string(1, c); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// }}}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.setf(ios::fixed), cout.precision(9);
  long long n, K;
  cin >> n >> K;
  vector<vector<int>> adj(n, vector<int>(n));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cin >> adj[i][j];

  const int maxK = floor(log2(K)) + 1;
  debug(maxK);
  vector<vector<vector<mint>>> dp(n,
                                  vector<vector<mint>>(n, vector<mint>(maxK)));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) dp[i][j][0] = adj[i][j];
  for (int k = 0; k < maxK; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) {
        if (k == 0) {
          dp[i][j][0] = adj[i][j];
        } else {
          for (int l = 0; l < n; ++l)
            dp[i][j][k] += dp[i][l][k - 1] * dp[l][j][k - 1];
        }
      }
  bool first = false;
  vector<vector<mint>> pathForK(n, vector<mint>(n));
  for (int k = 0; k < maxK; ++k)
    if (K & (1ll << k)) {
      if (!first) {
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j) pathForK[i][j] = dp[i][j][k];
        first = true;
      } else {
        vector<vector<mint>> newPathForK(n, vector<mint>(n));
        for (int i = 0; i < n; ++i)
          for (int j = 0; j < n; ++j)
            for (int l = 0; l < n; ++l) {
              newPathForK[i][j] += pathForK[i][l] * dp[l][j][k];
            }
        pathForK.swap(newPathForK);
      }
    }
  mint ans = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) ans += pathForK[i][j];
  cout << ans << '\n';
}


Measured memory_bytes: 1024