#include <cstdio>
#include <cstdlib>

using namespace std;

const int mod = 1000000007;
const long long mod2 = (long long)mod * mod;

class Matrix {
    public:
    
    int n;
    int m;
    int *data;
    
    Matrix(int n, int m) {
        this->n = n;
        this->m = m;
        data = (int *)calloc(n * m, sizeof(int));
    }
    
    Matrix(const Matrix &a) {
        n = a.n;
        m = a.m;
        data = (int *)malloc(sizeof(int) * n * m);
        
        for (int i = 0; i < n * m; i++) data[i] = a.data[i];
    }
    
    ~Matrix() {
        free(data);
    }
    
    Matrix& operator=(const Matrix &a) {
        free(data);
        
        n = a.n;
        m = a.m;
        data = (int *)malloc(sizeof(int) * n * m);
        
        for (int i = 0; i < n * m; i++) data[i] = a.data[i];
        
        return *this;
    }
    
    int* operator[](int x) {
        return data + x * m;
    }
    
    void identify(void) {
        for (int i = 0, j = 0; i < n; i++, j += m + 1) data[j] = 1;
    }
    
    const Matrix operator*(const Matrix &a) const {
        int *tmp = (int *)malloc(sizeof(int) * a.n * a.m);
        Matrix ans(n, a.m);
        
        for (int i = 0; i < a.m; i++) {
            for (int j = 0; j < a.n; j++) {
                tmp[i * a.n + j] = a.data[j * a.m + i];
            }
        }
        
        for (int i = 0, l = 0; i < n; i++) {
            int *p1 = data + i * m;
            int *p2 = tmp;
            
            for (int j = 0; j < a.m; j++, l++, p2 += m) {
                int *pa = p1;
                int *pb = p2;
                long long sum = 0;
                
                for (int k = 0; k < m; k++, pa++, pb++) {
                    sum += (long long)*pa * *pb;
                    if (sum >= mod2) sum -= mod2;
                }
                
                ans.data[l] = sum % mod;
            }
        }
        
        free(tmp);
        
        return ans;
    }
    
    void operator*=(const Matrix &a) {
        int *tmp = (int *)malloc(sizeof(int) * n * n);
        int *ans = (int *)malloc(sizeof(int) * n * n);
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp[i * n + j] = a.data[j * n + i];
            }
        }
        
        for (int i = 0, l = 0; i < n; i++) {
            int *p1 = data + i * n;
            int *p2 = tmp;
            
            for (int j = 0; j < n; j++, l++, p2 += n) {
                int *pa = p1;
                int *pb = p2;
                long long sum = 0;
                
                for (int k = 0; k < n; k++, pa++, pb++) {
                    sum += (long long)*pa * *pb;
                    if (sum >= mod2) sum -= mod2;
                }
                
                ans[l] = sum % mod;
            }
        }
        
        for (int i = 0; i < n * n; i++) data[i] = ans[i];
        
        free(tmp);
        free(ans);
    }
    
    const Matrix operator^(long long x) const {
        Matrix a = *this;
        Matrix ans(n, m);
        
        ans.identify();
        
        while (x) {
            if (x & 1) ans *= a;
            a *= a;
            x >>= 1;
        }
        
        return ans;
    }
};

int main() {
    int n, i, j;
    long long k, ans = 0;
    
    scanf("%d %lld", &n, &k);
    
    Matrix m(n, n);
    
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            int x;
            
            scanf("%d", &x);
            
            m[i][j] = x;
        }
    }
    
    m = m ^ k;
    
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            ans += m[i][j];
        }
    }
    
    printf("%lld\n", ans % mod);
    
    return 0;
}


Measured memory_bytes: 256