#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "256MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

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

;; BEGIN_INSERTED_CONTENTS
(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

;;;
;;; Multiset structure by Trie
;;;

;; ASCII code:
;; #\A: 65
;; #\a: 97
;; #\0: 48
(declaim (inline trie-char-encode))
(defun trie-char-encode (x)
  (- (char-code x) #.(char-code #\a)))

(defconstant +trie-alphabet-size+ 26)

(declaim (inline %make-trie-node))
(defstruct (trie-node (:constructor %make-trie-node
                          (&aux
                           (dp (make-array #.+trie-alphabet-size+
                                           :element-type 'uint32
                                           :initial-element 0))
                           (children (make-array #.+trie-alphabet-size+
                                                      :element-type t
                                                      :initial-element 0)))))
  (size 0 :type (integer 0 #.most-positive-fixnum))
  (dp 0 :type (simple-array uint32 (*)))
  (children nil :type (simple-array t (#.+trie-alphabet-size+))))

(declaim (inline make-trie))
(defun make-trie () (%make-trie-node))

(declaim (inline trie-add!))
(defun trie-add! (trie-node string)
  "Adds STRING to the trie."
  (declare (vector string))
  (let ((end (length string)))
    (labels ((recur (node position)
               (incf (trie-node-size node))
               (unless (= position end)
                 (let ((children (trie-node-children node))
                       (char (trie-char-encode (aref string position))))
                   (when (eql 0 (aref children char))
                     (setf (aref children char) (%make-trie-node)))
                   (recur (aref children char) (+ 1 position))))))
      (recur trie-node 0)
      trie-node)))

(declaim (inline trie-get))
(defun trie-get (trie-node string)
  (declare (simple-base-string string))
  (let* ((end (length string))
         (end-char (trie-char-encode (aref string (- end 1))))
         (end-1 (- end 1)))
    (labels ((recur (node position)
               (if (= position end-1)
                   (let ((dp (trie-node-dp node)))
                     (return-from trie-get (aref dp end-char)))
                   (let ((children (trie-node-children node))
                         (char (trie-char-encode (aref string position))))
                     (if (eql 0 (aref children char))
                         0
                         (recur (aref children char) (+ 1 position)))))))
      (recur trie-node 0))))

(defun trie-build (node)
  (declare #.OPT)
  (labels ((recur (node)
             (let ((children (trie-node-children node))
                   (dp (trie-node-dp node)))
               (declare ((simple-array uint32 (*)) dp))
               (dotimes (c +trie-alphabet-size+)
                 (unless (eq 0 (aref children c))
                   (recur (aref children c))
                   (let ((children-dp (trie-node-dp (aref children c))))
                     (declare ((simple-array uint32 (*)) dp))
                     (dotimes (c2 +trie-alphabet-size+)
                       (incf (aref dp c2)
                             (the fixnum (if (= c c2)
                                             (trie-node-size (aref children c))
                                             (aref children-dp c2)))))))))))
    (recur node)))

(in-package :cl-user)

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

(declaim (inline println))
(defun 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))
         (ss (make-array n :element-type 'simple-base-string))
         (trie (make-trie))
         (res 0))
    (declare (uint62 res))
    (dotimes (i n)
      (setf (aref ss i) (nreverse (coerce (read-line) 'simple-base-string)))
      (trie-add! trie (the simple-base-string (aref ss i))))
    (trie-build trie)
    (dotimes (i n)
      (let ((s (aref ss i)))
        (incf res (trie-get trie s))))
    (decf res n)
    (println res)))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    #+os-windows (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)
    #+os-unix (run-program "xsel" '("-b" "-o") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let* ((*standard-output* (or out (make-string-output-stream)))
         (res (etypecase thing
                (null
                 (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
                   (main)))
                (string
                 (with-input-from-string (*standard-input* (delete #\Return thing))
                   (main)))
                (symbol (5am:run! thing))
                (pathname
                 (with-open-file (*standard-input* thing)
                   (main))))))
    (if out res (get-output-stream-string *standard-output*))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (equal "1
"
          (run "3
abcxyx
cyx
abc
" nil)))
  (it.bese.fiveam:is
   (equal "5
"
          (run "6
b
a
abc
c
d
ab
" nil))))


Measured memory_bytes: 537604