Problem:
Score : 700 points

Problem Statement

Limak can repeatedly remove one of the first two characters of a string,
for example abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx.

You are given N different strings S_1, S_2, \ldots, S_N.
Among N \cdot (N-1) / 2 pairs (S_i, S_j),
in how many pairs could Limak obtain one string from the other?

Constraints

2 \leq N \leq 200\,000

S_i consists of lowercase English letters a-z.

S_i \neq S_j

1 \leq |S_i|

|S_1| + |S_2| + \ldots + |S_N| \leq 10^6

Input

Input is given from Standard Input in the following format.

N
S_1
S_2
\vdots
S_N

Output

Print the number of unordered pairs (S_i, S_j)
where i \neq j and Limak can obtain one string from the other.

Sample Input 1

3
abcxyx
cyx
abc

Sample Output 1

1

The only good pair is (abcxyx, cyx).

Sample Input 2

6
b
a
abc
c
d
ab

Sample Output 2

5

There are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab).

Solution in C# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

using System;
using CompLib.Collections;
using CompLib.Util;

public class Program
{
    private int N;
    private string[] S;
    private const long Mod1 = 1000000007;
    private const long Mod2 = (119 << 23) + 1;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        S = new string[N];
        for (int i = 0; i < N; i++)
        {
            S[i] = sc.Next();
        }

        Array.Sort(S, (l, r) => -l.Length.CompareTo(r.Length));

        // 1文字+のこり

        // 末尾 + 
        // 末尾が一致してる + 以前一致1文字
        // 

        var map = new HashMap<(long h1, long h2, int len, char ch), int>();
        long ans = 0;
        foreach (var t in S)
        {
            // tの末尾のハッシュ値
            long h1 = 0;
            long h2 = 0;
            int[] cnt = new int[26];
            for (int i = 1; i < t.Length; i++)
            {
                h1 *= 26;
                h2 *= 26;
                h1 += t[i] - 'a';
                h2 += t[i] - 'a';
                h1 %= Mod1;
                h2 %= Mod2;
                cnt[t[i] - 'a']++;
            }

            cnt[t[0] - 'a']++;
            ans += map[(h1, h2, t.Length - 1, t[0])];
            long p1 = 1;
            long p2 = 1;
            h1 = 0;
            h2 = 0;
            for (int i = 0; i < t.Length; i++)
            {
                int j = t.Length - i - 1;
                for (int c = 0; c < 26; c++)
                {
                    if (cnt[c] == 0) continue;
                    map[(h1, h2, i, (char) (c + 'a'))]++;
                }

                cnt[t[j] - 'a']--;
                h1 += (t[j] - 'a') * p1;
                h1 %= Mod1;
                h2 += (t[j] - 'a') * p2;
                h2 %= Mod2;
                p1 *= 26;
                p1 %= Mod1;
                p2 *= 26;
                p2 %= Mod2;
            }
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Collections
{
    using System.Collections.Generic;

    public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get
            {
                TValue o;
                return TryGetValue(key, out o) ? o : default(TValue);
            }
            set { base[key] = value; }
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}