#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;

#define LEN(x) (int)x.size()
#define ALL(x) x.begin(), x.end()

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

template<typename T, typename M = __gnu_pbds::null_type>
using ordered_set = __gnu_pbds::tree<T, M, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

inline int fcmp(double x, double y)
{ return (x < y + EPS ? (x < y - EPS ? -1 : 0) : 1);}

template<class base_t, const int M, const int N>
class matrix
{
private:
    base_t **t;

public:
    matrix(base_t c = 0)
    {
        t = new base_t*[M];
        for(int i = 0; i < M; i++)
        {
            t[i] = new base_t[N];
            for(int j = 0; j < N; j++)
                t[i][j] = 0;
            t[i][i] = c;
        }
    }

    base_t* & operator [] (const int i) { return t[i];}

    matrix<base_t, M, N> operator + (matrix<base_t, M, N>& b)
    {
        matrix<base_t, M, N> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[i][j] = t[i][j] + b[i][j];
        return ans;
    }
    matrix<base_t, M, N>& operator += (matrix<base_t, M, N>& b)
    {
        return *this = *this + b;
    }

    matrix<base_t, M, N> operator - (matrix<base_t, M, N>& b)
    {
        matrix<base_t, M, N> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[i][j] = t[i][j] - b[i][j];
        return ans;
    }
    matrix<base_t, M, N>& operator -= (matrix<base_t, M, N>& b)
    {
        return *this = *this - b;
    }

    template<const int U>
    matrix<base_t, M, U> operator * (matrix<base_t, N, U>& b)
    {
        matrix<base_t, M, U> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < U; j++)
                for(int k = 0; k < N; k++)
                    ans[i][j] += t[i][k] * b[k][j];
        return ans;
    }
    template<const int U>
    matrix<base_t, M, U>& operator *= (matrix<base_t, N, U>& b)
    {
        return *this = *this * b;
    }

    matrix<base_t, M, N> operator + (base_t b)
    {
        matrix<base_t, M, N> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[i][j] = t[i][j] + b;
        return ans;
    }
    matrix<base_t, M, N> operator - ( base_t b)
    {
        matrix<base_t, M, N> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[i][j] = t[i][j] - b;
        return ans;
    }
    matrix<base_t, M, N> operator * (base_t b)
    {
        matrix<base_t, M, N> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[i][j] = t[i][j] * b;
        return ans;
    }
    matrix<base_t, M, N> operator / (base_t b)
    {
        matrix<base_t, M, N> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[i][j] = t[i][j] / b;
        return ans;
    }

    matrix<base_t, N, M> tranpose()
    {
        matrix<base_t, N, M> ans;
        for(int i = 0; i < M; i++)
            for(int j = 0; j < N; j++)
                ans[j][i] = t[i][j];
        return ans;
    }

    base_t trace()
    {
        base_t sum = base_t(0);
        for(int i = 0; i < min(M, N); i++)
            sum += t[i][i];
        return sum;
    }
};

template<class base_t>
base_t inverse(base_t a, base_t m)
{
	base_t x = 0, y = 1;
	while(a > 0)
	{
		base_t t = m / a;
		m -= t * a; swap(a, m);
		x -= t * y; swap(x, y);
	}
	assert(m == 1);
	return x;
}

template<class base_t>
struct modint
{
	using mod_t = modint<base_t>;
	base_t val;
	static base_t mod;

	template<class T>
	inline base_t normalize(T n)
	{
		if(n >= mod || n < -mod)
			n %= mod;
		if(n < 0)
			n += mod;
		return (base_t)n;
	}

    modint(){ val = 0;}
	template<class T> modint(T x = 0) { val = normalize(x);}
	template<class T> explicit operator T() { return static_cast<T>(val);}
	const base_t operator ()() const { return val;}

	mod_t operator + (const mod_t& o)
	{
		base_t a = val + o(); if(a >= mod) a -= mod; return mod_t(a);
	}
	mod_t operator - (const mod_t& o)
	{
		base_t a = val - o(); if(a < 0) a += mod; return mod_t(a);
	}
	mod_t operator * (const mod_t& o)
	{
		if(is_same<base_t, int>::value)
			return normalize((ll)val * (ll)o());
		if(is_same<base_t, ll>::value)
		{
			ll x = (ll)((long double)val * o() / mod);
			ll y = (ll)((long double)val * o() - x * mod);
			return y;
		}
		assert(true);
	}
	mod_t operator / (const mod_t& o) { return *this * inverse(o(), mod);}

	mod_t& operator += (const mod_t& o) { return *this = *this + o;}
	mod_t& operator -= (const mod_t& o)	{ return *this = *this - o;}
	mod_t& operator *= (const mod_t& o)	{ return *this = *this * o;}
	mod_t& operator /= (const mod_t& o)	{ return *this = *this / o;}

	template<class T>
	mod_t& operator += (const T& o) { return *this = *this + mod_t(o);}
	template<class T>
	mod_t& operator -= (const T& o) { return *this = *this - mod_t(o);}
	template<class T>
	mod_t& operator *= (const T& o) { return *this = *this * mod_t(o);}
	template<class T>
	mod_t& operator /= (const T& o) { return *this = *this / mod_t(o);}
	
	mod_t& operator ++ () { return *this += 1;}
	mod_t& operator -- () { return *this -= 1;}
	mod_t operator ++ (int) { mod_t old(val); *this += 1; return old;}
	mod_t operator -- (int) { mod_t old(val); *this -= 1; return old;}

	bool operator == (const mod_t& o) { return val == o();}
	bool operator != (const mod_t& o) { return val != o();}
	bool operator < (const mod_t& o) { return val < o();}
	bool operator > (const mod_t& o) { return val > o();}
	bool operator <= (const mod_t& o) { return val <= o();}
	bool operator >= (const mod_t& o) { return val >= o();}

	friend istream& operator >> (istream& in, mod_t& o)
	{
		ll x; in >> x; o = mod_t(x); return in;		
	}

	friend ostream& operator << (ostream& out, const mod_t& o)
	{
		out << o(); return out;		
	}
};

template<class T, class base_t>
modint<base_t> operator + (T a, const modint<base_t>& b) { return a + b();}
template<class T, class base_t>
modint<base_t> operator - (T a, const modint<base_t>& b) { return a - b();}
template<class T, class base_t>
modint<base_t> operator * (T a, const modint<base_t>& b) { return a * b();}
template<class T, class base_t>
modint<base_t> operator / (T a, const modint<base_t>& b) { return a / b();}

template<class T, class base_t>
bool operator == (T a, const modint<base_t>& b) { return a == b();}
template<class T, class base_t>
bool operator != (T a, const modint<base_t>& b) { return a != b();}
template<class T, class base_t>
bool operator < (T a, const modint<base_t>& b) { return a < b();}
template<class T, class base_t>
bool operator <= (T a, const modint<base_t>& b) { return a <= b();}
template<class T, class base_t>
bool operator > (T a, const modint<base_t>& b) { return a > b();}
template<class T, class base_t>
bool operator >= (T a, const modint<base_t>& b) { return a >= b();}

template<class base_t>
base_t modint<base_t>::mod(MOD);
// template<class base_t>
// base_t& md = modint<base_t>::mod;

template<class base_t>
base_t fexp(base_t b, ll x)
{
    base_t ans(1);

    while(x)
    {
        if(x & 1LL) ans = ans * b;
        b = b * b;
        x >>= 1LL;
    }

    return ans;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cout.precision(10);
    
    int n; ll k;
    cin >> n >> k;

    matrix<modint<int>, 50, 50> mat;

    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin >> mat[i][j];

    mat = fexp(mat, k);
    
    modint<int> ans = 0;

    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            ans += mat[i][j];

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1536