The Chinese (Lunar) New Year festivities are in full swing along the West Kowloon Waterfront Promenade. During the Lantern Festival, the day marking the end of the Lunar New Year, a popular activity at night is known as "caai dang mai" (猜燈謎), i.e. "guessing lantern riddles", where children go out at night carrying paper lanterns and solve riddles on the lanterns, often related to guessing meanings of phrases. These riddles, written on slips of paper and attached to the lanterns, offer a fun challenge for young and old alike.
Lantern riddles (© Epoch Group Limited) This year, a particularly interesting lantern riddle has captured everyone's attention. Instead of traditional riddles displayed on one lantern, there is a row of $$$N$$$ consecutive lanterns labelled 1, 2, ..., $$$N$$$, where the $$$i$$$-th lantern ($$$1 \leq i \leq N$$$) displays a string of lowercase characters $$$S_i$$$. It is guaranteed that all $$$S_i$$$ are distinct. The challenge is to find combinations of lanterns that satisfy a special string triangle inequality.
We say that three strings $$$X$$$, $$$Y$$$, $$$Z$$$ satisfy the string triangle inequality if and only if for every permutation $$$[X', Y', Z']$$$ of $$$[X, Y, Z]$$$, the following condition is satisfied: $$$X' + Y' \gt Z'$$$. In other words, all of the following must be satisfied:
Here, + denotes the string concatenation operator, while > denotes the "lexicographically larger than" comparison operator (the same way + and > operate on strings in C++).
The lantern riddle is: count the number of tuples $$$(i, j, k)$$$ where $$$1 \leq i \lt j \lt k \leq N$$$ and the strings $$$S_i, S_j, S_k$$$ satisfy the string triangle inequality.
The first line contains one integer $$$N$$$ ($$$3 \leq N \leq 2 \times 10^5$$$), representing the number of lanterns.
The next $$$N$$$ lines each contain a sequence of lowercase characters $$$S_i$$$ ($$$3 \leq \sum |S_i| \leq 10^6$$$), representing the strings on the lanterns.
It is guaranteed that all $$$S_i$$$ are distinct.
Output a single integer, the number of tuples satisfying the requirement.
5aaaaaaaaaaaaaaaaaaaa
7
3abc
0
Sample 1:
All strings contain the character a only. Hence, the problem is equivalent to finding the number of tuples of integers $$$(a,b,c)$$$ such that $$$2 \le a \lt b \lt c \le 6$$$ and the usual triangle inequality for integers is satisfied. The valid tuples are $$$(2, 3, 4), (2, 4, 5), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)$$$.
Sample 2:
a + b = ab < c. Therefore there are no valid tuples.
| Name |
|---|


