#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<ll mod>
struct modint
{
    ll val;
    
    inline ll normalize(ll n)
    {
        if(n >= mod || n < -mod)
            n %= mod;
        if(n < 0)
            n += mod;
        return 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);}

    modint<mod>& operator += (const modint<mod> o) { return *this = modint<mod>(val + o.val);}   
    template<class T> modint<mod>& operator += (const T o) { return *this = modint<mod>(val + normalize(o));}   
    modint<mod>& operator -= (const modint<mod> o) { return *this = modint<mod>(val - o.val);}   
    template<class T> modint<mod>& operator -= (const T o) { return *this = modint<mod>(val - normalize(o));}   
    modint<mod>& operator *= (const modint<mod> o) { return *this = modint<mod>(val * o.val);}   
    template<class T> modint<mod>& operator *= (const T o) { return *this = modint<mod>(val * normalize(o));}

    modint<mod>& operator++() { return *this += 1;}
    modint<mod>& operator--() { return *this -= 1;}

    modint<mod> operator + (const modint<mod> o) { return modint<mod>(val + o.val);}   
    template<class T> modint<mod> operator + (const T o) { return modint<mod>(val + o);}   
    modint<mod> operator - (const modint<mod> o) { return modint<mod>(val - o.val);}   
    template<class T> modint<mod> operator - (const T o) { return modint<mod>(val - o);}   
    modint<mod> operator * (const modint<mod> o) { return modint<mod>(val * o.val);}   
    template<class T> modint<mod> operator * (const T o) { return modint<mod>(val * o);}

    bool operator == (const modint<mod> o) { return val == o.val;}
    template<class T> bool operator == (const T o) { return val == normalize(o);}
    bool operator != (const modint<mod> o) { return val != o.val; }
    template<class T> bool operator != (const T o) { return val != normalize(o);}

    bool operator < (const modint<mod> o) { return val < o.val;}
    template<class T> bool operator < (const T o) { return val < normalize(o);}

    modint<mod>& operator /= (const modint<mod> o) { return *this *= o.inv();}
    template<class T> modint<mod>& operator /= (const T o) { return *this *= modint<mod>(o).inv();}
    modint<mod> operator / (modint<mod> o) {modint<mod> ans(val); return ans * o.inv();}
    template<class T> modint<mod> operator / (T o) { return modint<mod>(val) / modint<mod>(o);}

    modint<mod> inv() { return fexp(*this, mod - 2);}

    friend istream& operator >> (istream& in, modint<mod>& n)
    {
        ll x; in >> x;
        n = modint<mod>(x);
        return in;
    }

    friend ostream& operator << (ostream& out, const modint<mod>& n)
    {
        return out << n.val;
    }
};

template<class T>
T fexp(T b, ll x)
{
    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<MOD>, 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<MOD> 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: 2688