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: Accepted)

using System;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using System.Collections.Generic;

class Myon

{
    public Myon() { }
    public static int Main()
    {
        cin = new Scanner();
        new Myon().calc();
        return 0;
    }
    static Scanner cin;

    class Node
    {
        public Node[] Next;
        public bool exist;
        public Node() {
            Next = new Node[26];
        }
    }



    void calc()
    {
        int N = cin.nextInt();
        string[] S = new string[N];
        for (int i = 0; i < N; i++)
        {
            S[i] = cin.next();
        }
        IComparer<string> strlenComp = new StrLenComparer();
        Array.Sort(S, strlenComp);

        Node FirstNode = new Node();

        long ans = 0;

        for (int i = 0; i < N; i++)
        {
            Node NowNode = FirstNode;
            int[] num = new int[26];
            bool active = true;

            foreach(var c in S[i])
            {
                num[c - 'a']++;
            }

            for (int j = S[i].Length - 1; j >= 0; j--)
            {
                if (active)
                {
                    for (int k = 0; k < 26; k++)
                    {
                        if (num[k] == 0) continue;
                        if (NowNode.Next[k] != null && NowNode.Next[k].exist) ans++; 
                    }
                }
                char c = S[i][j];
                int p = c - 'a';
                num[p]--;
                if(NowNode.Next[p] == null)
                {
                    active = false;
                    NowNode.Next[p] = new Node();
                }
                NowNode = NowNode.Next[p];

            }
            NowNode.exist = true;
        }


        Console.WriteLine(ans);

    }

    public class StrLenComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return x.Length - y.Length;
        }
    }
}


class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}