program atcoder
    implicit none
    integer(8) :: n,k,i,j
    integer(8),allocatable,dimension(:,:) :: a,b
    read*,n,k
    allocate(a(n,n),b(n,n))
    do i=1,n
        read(*,*)(a(i,j),j=1,n)
    enddo
    do i=1,n
        do j=1,n
            b(i,j) = 0
            if(i==j)then
                b(i,j) = 1
            endif
        enddo
    enddo
    do while (k/=0)
        if(mod(k,2_8)==1)then
            b = my_matmul(b,a,n)
            b = mod(b,1000000007_8)
        endif
        a = my_matmul(a,a,n)
        a = mod(a,1000000007_8)
        k = ishft(k,-1)
    enddo
    write(*,'(I0)')mod(sum(b),1000000007_8)
    stop 
contains

function my_matmul(x,y,n) result(a)
    implicit none
    integer(8) :: i,j,k,n
    integer(8),dimension(n,n) :: x,y
    integer(8),allocatable,dimension(:,:) :: a
    allocate(a(n,n))
    a = 0
    do i=1,n
        do j=1,n
            do k=1,n
                a(i,j) = mod(a(i,j)+x(i,k)*y(k,j),1000000007_8)
            enddo
        enddo
    enddo
end function my_matmul

end program atcoder