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: Wrong Answer)

using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;

public class Class {
    static void Main(string[] args) {
        var N = ReadValue<int>();
        var S = new string[N];
        long ans = 0;
        for (var i = 0; i < N; i++) S[i] = ReadValue<string>(); ;
        
        for(var R = 0; R < N; R++) {
            for(var L = R + 1; L < N; L++) {
                if(S[R].Length > S[L].Length) {
                    if (S[L].Length > 1) {
                        var S1 = S[L].Substring(1);
                        var A1 = S[R].IndexOf(S[L].Substring(0, 1));
                        var A2 = S[R].IndexOf(S1);
                        if (A1 >= 0 && A2 >= 0) {
                            if (A1 < A2 && A2 + S1.Length == S[R].Length) ans++;
                        }
                    } else {
                        if (S[R].IndexOf(S[L]) >= 0) ans++;
                    }
                } else if (S[R].Length < S[L].Length) {
                    if (S[R].Length > 1) {
                        var S1 = S[R].Substring(1);
                        var A1 = S[L].IndexOf(S[R].Substring(0, 1));
                        var A2 = S[L].IndexOf(S1);
                        if (A1 >= 0 && A2 >= 0) {
                            if (A1 < A2 && A2 + S1.Length == S[L].Length) ans++;
                        }
                    } else {
                        if (S[L].IndexOf(S[R]) >= 0) ans++;
                    }
                } else if (S[R].Length == S[L].Length) {
                    if (S[R] == S[L]) ans++;
                }
                //Console.WriteLine("{0} {1}",S[R],S[L]);
                //Console.WriteLine(ans);
            }
        }
        Console.Write(ans);
    }

    static T1 ReadValue<T1>() {
        var t1 = TypeDescriptor.GetConverter(typeof(T1));
        return (T1)t1.ConvertFromString(Console.ReadLine());
    }

    static (T1, T2) ReadValue<T1, T2>() {
        var rl = Console.ReadLine().Trim().Split().ToArray();
        var t1 = TypeDescriptor.GetConverter(typeof(T1));
        var t2 = TypeDescriptor.GetConverter(typeof(T2));
        return ((T1)t1.ConvertFromString(rl[0]),
                (T2)t2.ConvertFromString(rl[1]));
    }

    static (T1, T2, T3) ReadValue<T1, T2, T3>() {
        var rl = Console.ReadLine().Trim().Split().ToArray();
        var t1 = TypeDescriptor.GetConverter(typeof(T1));
        var t2 = TypeDescriptor.GetConverter(typeof(T2));
        var t3 = TypeDescriptor.GetConverter(typeof(T3));
        return ((T1)t1.ConvertFromString(rl[0]),
                (T2)t2.ConvertFromString(rl[1]),
                (T3)t3.ConvertFromString(rl[2]));
    }

    static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>() {
        var rl = Console.ReadLine().Trim().Split().ToArray();
        var t1 = TypeDescriptor.GetConverter(typeof(T1));
        var t2 = TypeDescriptor.GetConverter(typeof(T2));
        var t3 = TypeDescriptor.GetConverter(typeof(T3));
        var t4 = TypeDescriptor.GetConverter(typeof(T4));
        return ((T1)t1.ConvertFromString(rl[0]),
                (T2)t2.ConvertFromString(rl[1]),
                (T3)t3.ConvertFromString(rl[2]),
                (T4)t4.ConvertFromString(rl[3]));
    }

    static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>() {
        var rl = Console.ReadLine().Trim().Split().ToArray();
        var t1 = TypeDescriptor.GetConverter(typeof(T1));
        var t2 = TypeDescriptor.GetConverter(typeof(T2));
        var t3 = TypeDescriptor.GetConverter(typeof(T3));
        var t4 = TypeDescriptor.GetConverter(typeof(T4));
        var t5 = TypeDescriptor.GetConverter(typeof(T5));
        return ((T1)t1.ConvertFromString(rl[0]),
                (T2)t2.ConvertFromString(rl[1]),
                (T3)t3.ConvertFromString(rl[2]),
                (T4)t4.ConvertFromString(rl[3]),
                (T5)t5.ConvertFromString(rl[4]));
    }

    static string[] stringArray => Console.ReadLine().Split().ToArray();
    static int[] intArray => Console.ReadLine().Trim().Split().Select(int.Parse).ToArray();
    static long[] longArray => Console.ReadLine().Trim().Split().Select(long.Parse).ToArray();
    static double[] doubleArray => Console.ReadLine().Trim().Split().Select(double.Parse).ToArray();
    static decimal[] decimalArray => Console.ReadLine().Trim().Split().Select(decimal.Parse).ToArray();
}