#P4596. Word Square Count

    ID: 17842 Type: Default 1000ms 256MiB

Word Square Count

Word Square Count

You are given N words, all having the same length. Four distinct words can be arranged in order (one per row) to form a square. In other words, if you place the four words one below the other, they form a square grid of size L × L, where L is the length of each word.

Your task is to count how many different squares can be formed by selecting 4 distinct words from the provided list. Note that even if two words differ in only one character they are considered different. Also, the same word cannot be used more than once in the same square.

The answer can be mathematically computed as:

\(\text{Answer} = N \times (N-1) \times (N-2) \times (N-3)\)

If N < 4, output 0.

inputFormat

The first line contains an integer N (1 ≤ N ≤ 105), the number of words.

The following N lines each contain a single word. All words have the same length and consist only of uppercase or lowercase English letters.

outputFormat

Output a single integer denoting the number of different squares that can be formed by selecting 4 distinct words.

sample

4
HLAD
NIVA
HSIN
DEDA
24