#define NDEBUG
NDEBUG


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstring>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <memory>
#include <queue>
#include <random>


using namespace std;

template<class T>
using vec = std::vector<T>;

template<class T>
using vec2d = std::vector<std::vector<T>>;


template<class T>
vec2d<T> makeVec2d(const int n1, const int n2) {
  return vec2d<T>(n1, vec<T>(n2));
}


#define FOR(i, n) for (int i = 0; i < (n); ++i)


// TC_REMOVE_BEGIN
/// caide keep
bool __hack = std::ios::sync_with_stdio(false);
/// caide keep
auto __hack1 = cin.tie(nullptr);
// TC_REMOVE_END


// Section with adoption of array and vector algorithms.


//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//
//template <class T> using StdTree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;


namespace template_util {
    

    constexpr int bytecount(uint64_t x) {
        return x ? 1 + bytecount(x >> 8) : 0;
    }

    template<int N>
    struct bytetype {
        
    };

    
    /// caide keep
    template<uint64_t N>
    struct minimal_uint : bytetype<bytecount(N)> {
    };
}


template<class T>
T next(istream& in) {
    T ret;
    in >> ret;
    return ret;
}


//#include "collections/multivector.h"
//
//template <class T, template <class, size_t> class B1, template <class, size_t> class B2>
//vec<T, 2> operator*(multivec_impl::vec_mixin<B1, T, 2>& a, multivec_impl::vec_mixin<B2, T, 2>& a) {
//    size_t n = a.template dim<0>();
//    size_t n1 = a.template dim<1>();
//    size_t m = a.template dim<0>();
//    size_t m1 = a.template dim<1>();
//    assert(n1 == m);
//    auto ret = make_vec<T>(n, m1);
//    forn (size_t, i, n) {
//        forn (size_t, j, m1) {
//            T sum = {};
//            forn (size_t, k, n1) {
//                sum += a(i, k) * a(k, j);
//            }
//            ret(i, j) = sum;
//        }
//    }
//    return ret;
//};
//
//template <class T, template <class, size_t> class B2>
//vec<T, 2>& operator*=(vec<T, 2>& a, multivec_impl::vec_mixin<B2, T, 2>& a) {
//    return a = a * a;
//};
//
//template <class T, template <class, size_t> class B1, template <class, size_t> class B2>
//vec<T, 2> operator+(multivec_impl::vec_mixin<B1, T, 2>& a, multivec_impl::vec_mixin<B2, T, 2>& a) {
//    size_t n = a.template dim<0>();
//    size_t n1 = a.template dim<1>();
//    size_t m = a.template dim<0>();
//    size_t m1 = a.template dim<1>();
//    assert(n == m && n1 == m1);
//    auto ret = make_vec<T>(n, n1);
//    forn (size_t, i, n) {
//        forn (size_t, j, n1) {
//            ret(i, j) = a(i, j) + a(i, j);
//        }
//    }
//    return ret;
//};
//
//template <class T, template <class, size_t> class B2>
//vec<T, 2>& operator+=(vec<T, 2>& a, multivec_impl::vec_mixin<B2, T, 2>& a) {
//    size_t n = a.template dim<0>();
//    size_t n1 = a.template dim<1>();
//    size_t m = a.template dim<0>();
//    size_t m1 = a.template dim<1>();
//    assert(n == m && n1 == m1);
//    forn (size_t, i, n) {
//        forn (size_t, j, n1) {
//            a(i, j) += a(i, j);
//        }
//    }
//    return a;
//};
//
//template <class T, template <class, size_t> class B1, template <class, size_t> class B2>
//vec<T, 2> operator-(multivec_impl::vec_mixin<B1, T, 2>& a, multivec_impl::vec_mixin<B2, T, 2>& a) {
//    size_t n = a.template dim<0>();
//    size_t n1 = a.template dim<1>();
//    size_t m = a.template dim<0>();
//    size_t m1 = a.template dim<1>();
//    assert(n == m && n1 == m1);
//    auto ret = make_vec<T>(n, n1);
//    forn (size_t, i, n) {
//        forn (size_t, j, n1) {
//            ret(i, j) = a(i, j) - a(i, j);
//        }
//    }
//    return ret;
//};
//
//template <class T, template <class, size_t> class B2>
//vec<T, 2>& operator-=(vec<T, 2>& a, multivec_impl::vec_mixin<B2, T, 2>& a) {
//    size_t n = a.template dim<0>();
//    size_t n1 = a.template dim<1>();
//    size_t m = a.template dim<0>();
//    size_t m1 = a.template dim<1>();
//    assert(n == m && n1 == m1);
//    forn (size_t, i, n) {
//        forn (size_t, j, n1) {
//            a(i, j) -= a(i, j);
//        }
//    }
//    return a;
//};
//
//template <class T, template <class, size_t> class B1>
//vec<T, 2> operator-(multivec_impl::vec_mixin<B1, T, 2>& a) {
//    size_t n = a.template dim<0>();
//    size_t n1 = a.template dim<1>();
//    auto ret = make_vec<T>(n, n1);
//    forn (size_t, i, n) {
//        forn (size_t, j, n1) {
//            ret(i, j) = -a(i, j);
//        }
//    }
//    return ret;
//};

namespace matrix_utils {


template<class T>
vector<vector<T>> Mul(const vector<vector<T>>& a, const vector<vector<T>>& b, const T MOD) {
    vector<vector<T>> res(a.size(), vector<T>(b[0].size(), 0));
    FOR(i, a.size()) {
        FOR(j, b[0].size()) {
            FOR(k, a[0].size()) {
                res[i][j] += (a[i][k] * b[k][j]) % MOD;
                if (res[i][j] >= MOD) {
                    res[i][j] -= MOD;
                }
            }
        }
    }
    return res;
}


template<class T>
vector<vector<T>> Pow(vector<vector<T>> a, int64_t p, const T MOD) {
    const int n = (int)a.size();
    vector<vector<T>> res(n, vector<T>(n, 0));
    for (int i = 0; i < n; ++i) res[i][i] = 1;
    for (; p; p /= 2) {
        if (p & 1) res = Mul<T>(res, a, MOD);
        a = Mul<T>(a, a, MOD);
    }
    return res;
}

}

const int64_t MOD = (int64_t)1e9 + 7;

void solve(istream& in, ostream& out) {
  auto n = next<int>(in);
  auto k = next<int64_t>(in);
  vec2d<int64_t> a = makeVec2d<int64_t>(n, n);
  FOR(i, n) {
    FOR(j, n) {
      a[i][j] = next<int64_t>(in);
    }
  }
  a = matrix_utils::Pow(a, k, MOD);
  int64_t res = 0;
  FOR(i, n) {
    FOR(j, n) {
      res += a[i][j];
      if (res >= MOD) {
        res -= MOD;
      }
    }
  }
  out << res << "\n";
}

//#include <fstream>


int main() {
//  ifstream fin("bridges.in");
//  ofstream fout("bridges.out");
    solve(cin, cout);
//  solve(fin, fout);
    return 0;
}



Measured memory_bytes: 384