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++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

template<int sigma, int a>
struct Trie{
private:
    template<int sig>
    struct Node{
        int chi[sig];
        int siz = 0;
        Node(){
            memset(chi, -1, sizeof(chi));
        }
    };
    vector<Node<sigma>> nodes;
public:
    Trie(){
        nodes.push_back(Node<sigma>());
    }
    void add_node(int n, int c){
        nodes[n].chi[c] = nodes.size();
        nodes.push_back(Node<sigma>());
    }
    void add(string &s){
        int n = 0;
        nodes[n].siz++;
        for(int i=0; i<s.size(); i++){
            if(nodes[n].chi[s[i]-a] == -1) add_node(n, s[i]-a);
            n = nodes[n].chi[s[i]-a];
            nodes[n].siz++;
        }
    }
    int count_prefix(string &s){
        int n = 0;
        for(int i=0; i<s.size(); i++){
            if(nodes[n].chi[s[i]-a] == -1) return 0;
            n = nodes[n].chi[s[i]-a];
        }
        return nodes[n].siz;
    }
};

int N;
vector<string> S[1000001];

Trie<26, 'a'> trie[26];
vector<pair<int, pair<int,int>>> query[1000001];

signed main(){
    cin >> N;
    rep(i, N){
        string s;
        cin >> s;
        reverse(all(s));
        S[s.size()].pb(s);
    }

    ll ans = 0;

    for(int i=1000000; i>=1; i--){
        rep(j, query[i-1].size()){
            trie[query[i-1][j].first].add(S[query[i-1][j].second.first][query[i-1][j].second.second]);
        }
        rep(j, S[i].size()){
            string tmp = S[i][j].substr(0, S[i][j].size()-1);
            ans += trie[S[i][j].back()-'a'].count_prefix(tmp);
            int p[26];
            memset(p, -1, sizeof(p));
            rep(l, S[i][j].size()){
                p[S[i][j][l]-'a'] = l;
            }
            rep(l, 26){
                if(p[l] == i-1) trie[l].add(S[i][j]);
                else if(p[l] != -1) query[p[l]].pb({ l,{i,j } });
            }
        }
    }

    cout << ans << endl;
}