(defun %mod-expt (a n m) (let ((result 1)) (loop while (> n 0) when (oddp n) do (setq result (mod (* result a) m)) do (setq a (mod (* a a) m)) (setq n (ash n -1))) result)) (defun main (&rest argv) (declare (ignorable argv)) (let* ((n (read)) (k (read)) (m (read)) (p-arr (make-array n :element-type 'integer)) (e-arr (make-array n :element-type 'integer)) (a-arr (make-array n :element-type 'integer)) (h-arr (make-array n :element-type 'integer)) (res 0)) (dotimes (i n) (setf (aref p-arr i) (read))) (dotimes (i n) (setf (aref e-arr i) (read))) (dotimes (i n) (setf (aref a-arr i) (read))) (dotimes (i n) (setf (aref h-arr i) (read))) (sort p-arr #'<) (sort e-arr #'<) (sort a-arr #'<) (sort h-arr #'<) (dotimes (i n) (incf res (%mod-expt (- (max (aref p-arr i) (aref e-arr i) (aref a-arr i) (aref h-arr i)) (min (aref p-arr i) (aref e-arr i) (aref a-arr i) (aref h-arr i))) k m)) (setf res (mod res m))) (format t "~d~%" res))) (main)