;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defun read-fixnum ()
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char *standard-input* nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte *standard-input* nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (return-from read-fixnum 0))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (the fixnum (* result 10))))
              (return (if minus (- result) result))))))))

(declaim (inline generalized-power))
(defun generalized-power (base power op identity)
  (declare ((integer 0 #.most-positive-fixnum) power)
           (function op))
  (labels ((recur (x p)
             (declare ((integer 0 #.most-positive-fixnum) p))
             (cond ((zerop p) identity)
                   ((evenp p) (recur (funcall op x x) (ash p -1)))
                   (t (funcall op x (recur x (- p 1)))))))
    (recur base power)))

(declaim (inline gemm))
(defun gemm (a b &key (op+ #'+) (op* #'*) (identity+ 0))
  (declare ((simple-array * (* *)) a b)
           (function op+ op*))
  (let ((c (make-array (list (array-dimension a 0) (array-dimension b 1))
                       :element-type (array-element-type a))))
    (dotimes (col (array-dimension a 0))
      (dotimes (row (array-dimension b 1))
        (let ((res identity+))
          (dotimes (k (array-dimension a 1))
            (setf res
                  (funcall op+ res (funcall op* (aref a col k) (aref b k row)))))
          (setf (aref c col row) res))))
    c))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Body

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (adja (make-array (list n n) :element-type 'uint32))
         (iden (make-array (list n n) :element-type 'uint32 :initial-element 0)))
    (declare (uint32 n)
             (fixnum k))
    (dotimes (i n)
      (setf (aref iden i i) 1)
      (dotimes (j n)
        (setf (aref adja i j) (read-fixnum))))
    (let ((mat (generalized-power adja k
                                  (lambda (m1 m2)
                                    (declare ((simple-array uint32 (* *)) m1 m2))
                                    (gemm m1 m2
                                          :op+ (lambda (x y)
                                                 (declare (uint32 x y))
                                                 (mod (+ x y) +mod+))
                                          :op* (lambda (x y)
                                                 (declare (uint32 x y))
                                                 (mod (* x y) +mod+))))
                                  iden))
          (sum 0))
      (declare ((simple-array uint32 (* *)) mat)
               (uint32 sum))
      (dotimes (i n)
        (dotimes (j n)
          (setf sum (mod (+ sum (aref mat i j)) +mod+))))
      (println sum))))

#-swank(main)


Measured memory_bytes: 26976