#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 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;
}

ll mod = MOD;

template<class T>
T inverse(T b)
{
    return fexp(b, mod - 2);
}

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

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

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

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

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

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

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

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

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

    matrix<modint, 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 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