;; -*- 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 (ql:quickload '(:cl-debug-print :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (term-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)) ; declaring (sb-kernel:ansi-stream in) will be faster
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) term-char))
                (return (values buffer-string idx))))

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

(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
(declaim (inline histogram=))
(defun histogram= (h1 h2)
  (declare #.OPT)
  (and (= (sb-kernel:%vector-raw-bits h1 0)
          (sb-kernel:%vector-raw-bits h2 0))
       (= (sb-kernel:%vector-raw-bits h1 1)
          (sb-kernel:%vector-raw-bits h2 1))))

(declaim (inline sxhash-histogram))
(defun sxhash-histogram (h)
  (declare #.OPT
           ((simple-array uint4 (32)) h))
  (sb-int:mix (sb-kernel:%vector-raw-bits h 0)
              (sb-kernel:%vector-raw-bits h 1)))

(sb-ext:define-hash-table-test histogram= sxhash-histogram)

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (line (make-string 10 :element-type 'base-char))
         (table (make-hash-table :test 'histogram= :size n))
         (res 0))
    (declare (uint32 n)
             (uint62 res))
    (dotimes (i n)
      (read-line-into line)
      (let ((histo (make-array 32 :element-type 'uint4 :initial-element 0)))
        (loop for c across line
              do (incf (aref histo (- (char-code c) 97))))
        (if (gethash histo table)
            (incf (the uint32 (gethash histo table)))
            (setf (gethash histo table) 1))))
    (loop for value of-type uint31 being each hash-value of table
          do (incf res (floor (* value (- value 1)) 2)))
    (println res)))

#-swank (main)


Measured memory_bytes: 23012