(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
;; (with-memoizing (:hash-table :test #'equal :key #'cons)
;;   (defun ...))
;; (with-memoizing (:array (10 10 * 10) :initial-element -1 :element-type 'fixnum)
;;   (defun ...))
(defmacro with-memoizing (cache-attribs def-form)
  (let* ((cache-attribs (if (atom cache-attribs) (list cache-attribs) cache-attribs))
         (cache-type (first cache-attribs))
         (dimensions-with-* (when (eql cache-type :array) (second cache-attribs)))
         (dimensions (remove '* dimensions-with-*))
         (rank (length dimensions))
         (rest-attribs (ecase cache-type
                         (:hash-table (cdr cache-attribs))
                         (:array (cddr cache-attribs))))
         (key (prog1 (getf rest-attribs :key) (remf rest-attribs :key)))
         (cache-form (case cache-type
                       (:hash-table `(make-hash-table ,@rest-attribs))
                       (:array `(make-array ',dimensions ,@rest-attribs))))
         (initial-element (when (eql cache-type :array)
                            (assert (member :initial-element rest-attribs))
                            (getf rest-attribs :initial-element))))
    (let ((cache (gensym))
          (value (gensym))
	  (present-p (gensym))
          (name-alias (gensym))
	  (args-lst (gensym))
          (indices (loop repeat rank collect (gensym))))
      (labels ((make-cache-check-form (cache-type args)
                 (case cache-type
                   (:hash-table
                    `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
                       (multiple-value-bind (,value ,present-p)
                           (gethash ,args-lst ,cache)
                         (if ,present-p
                             ,value
                             (setf (gethash ,args-lst ,cache)
                                   (,name-alias ,@args))))))
                   (:array
                    (let ((memoized-args (loop for dimension in dimensions-with-*
                                               for arg in args
                                               unless (eql dimension '*)
                                               collect arg)))
                      (if key
                          `(multiple-value-bind ,indices
                               (funcall ,key ,@memoized-args)
                             (let ((,value (aref ,cache ,@indices)))
                               (if (eql ,initial-element ,value)
                                   (setf (aref ,cache ,@indices)
                                         (,name-alias ,@args))
                                   ,value)))
                          `(let ((,value (aref ,cache ,@memoized-args)))
                             (if (eql ,initial-element ,value)
                                 (setf (aref ,cache ,@memoized-args)
                                       (,name-alias ,@args))
                                 ,value)))))))
               (make-reset-form (cache-type)
                 (case cache-type
                   (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
                   (:array `(prog1 nil
                              (fill (array-storage-vector ,cache) ,initial-element)))))
               (make-reset-name (name)
                 (intern (format nil "RESET-~A" (symbol-name name))))
               (extract-declarations (body)
                 (remove-if-not (lambda (form) (eql 'declare (car form))) body)))
        (ecase (car def-form)
          ((defun)
           (destructuring-bind (_ name args &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (defun ,(make-reset-name name) () ,(make-reset-form cache-type))
                (defun ,name ,args
                  (labels ((,name-alias ,args ,@body))
                    (declare (inline ,name-alias))
                    ,@(extract-declarations body)
                    ,(make-cache-check-form cache-type args))))))
          ((nlet)
           (destructuring-bind (_ name bindings &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (nlet ,name ,bindings
                      ,(let ((args (mapcar (lambda (x) (if (atom x) x (car x))) bindings)))
                         `(labels ((,name-alias ,args ,@body))
                            (declare (inline ,name-alias))
                            ,@(extract-declarations body)
                            ,(make-cache-check-form cache-type args)))))))
          ((labels flet)
           (destructuring-bind (_ definitions &body labels-body) def-form
             (declare (ignore _))
             (destructuring-bind (name args &body body) (car definitions)
               `(let ((,cache ,cache-form))
                  (,(car def-form)
                   ((,(make-reset-name name) () ,(make-reset-form cache-type))
                    (,name ,args
                           (labels ((,name-alias ,args ,@body))
                             (declare (inline ,name-alias))
                             ,@(extract-declarations body)
                             ,(make-cache-check-form cache-type args)))
                    ,@(cdr definitions))
                   (declare (ignorable #',(make-reset-name name)))
                   ,@labels-body))))))))))

;; (test with-memoizing
;;   (finishes (macroexpand `(with-memoizing (:hash-table :test #'equal)
;;                             (defun add (x y) (+ x y)))))
;;   (finishes (macroexpand `(with-memoizing (:array '(10 10)
;;                                            :element-type 'fixnum
;;                                            :initial-element -1)
;;                             (defun add (x y) (+ x y)))))
;;   (finishes (macroexpand `(with-memoizing (:array '(10 10)
;;                                            :element-type 'fixnum
;;                                            :initial-element -1)
;;                             (labels ((add (x y) (+ x y))
;; 		                     (my-print (x) (print x)))
;; 	                      (add 1 2))))))

(defmacro nlet (name args &body body)
  (labels ((ensure-list (x) (if (listp x) x (list x))))
    (let ((args (mapcar #'ensure-list args)))
      `(labels ((,name ,(mapcar #'car args) ,@body))
         (,name ,@(mapcar #'cadr args))))))

(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))))

;; Hauptteil

(defun calc-combination-per-row (bridge-pos width)
  "Calculates the number of combinations per row for BRIDGE-POS.
bridge-pos ::=  0 |  ... | WIDTH-2"
  (loop for bits from 0 below (expt 2 (- width 1))
        count (and (= 1 (ldb (byte 1 bridge-pos) bits))
                   (loop repeat (- width 2)
                         for mask = #b11 then (ash mask 1)
                         never (= mask (logand bits mask))))))

(defun calc-combination-per-row2 (pos width)
  "Calculates the number of combinations per row for NO BRIDGE position.
pos ::=  0 |  ... | WIDTH-1"
  (loop for bits from 0 below (expt 2 (- width 1))
        count (and (= 0 (ldb (byte 1 (max (- pos 1) 0)) bits))
                   (= 0 (ldb (byte 1 (min pos (- width 2))) bits))
                   (loop repeat (- width 2)
                         for mask = #b11 then (ash mask 1)
                         never (= mask (logand bits mask))))))

(defconstant +magic+ #.(+ (expt 10 9) 7))

(defun solve (height width dest)
  ;; HEIGHT, DEST: 0-based
  (if (= width 1)
      (if (= dest 0) 1 0)
      (let ((comb-table-lr (loop for pos below (- width 1)
                                 collect (calc-combination-per-row pos width) into res
                                 finally (return (coerce res '(simple-array uint32 (*))))))
            (comb-table-mid (loop for pos below width
                                  collect (calc-combination-per-row2 pos width) into res
                                  finally (return (coerce res '(simple-array uint32 (*)))))))
        (with-memoizing (:array (101 9) :element-type 'fixnum :initial-element -1)
          (nlet recurse ((x height) (y dest))
            (cond ((zerop x)
                   (if (zerop y) 1 0))
                  ((zerop y)
                   (mod (+ (* (aref comb-table-mid 0)
                              (recurse (- x 1) y))
                           (* (aref comb-table-lr 0)
                              (recurse (- x 1) (+ y 1))))
                        +magic+))
                  ((= y (- width 1))
                   (mod (+ (* (aref comb-table-mid (- width 1))
                              (recurse (- x 1) y))
                           (* (aref comb-table-lr (- width 2))
                              (recurse (- x 1) (- y 1))))
                        +magic+))
                  (t (mod (+ (* (aref comb-table-mid y)
                                (recurse (- x 1) y))
                             (* (aref comb-table-lr (- y 1))
                                (recurse (- x 1) (- y 1)))
                             (* (aref comb-table-lr y)
                                (recurse (- x 1) (+ y 1))))
                          +magic+))))))))

(defun main ()
  (let* ((h (read))
         (w (read))
         (k (read)))
    (println (solve h w (- k 1)))))

#-swank(main)



Measured memory_bytes: 35428