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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

template <typename T>
struct RollingHash {
    using ll = long long;
    ll B_1, B_2, MOD_1, MOD_2;
    vector<ll> hash_1, hash_2, pow_1, pow_2;

    RollingHash(vector<T> v, ll B_1 = 1007, ll B_2 = 1009,
                ll MOD_1 = 1000000007, ll MOD_2 = 1000000009)
        : B_1(B_1), B_2(B_2), MOD_1(MOD_1), MOD_2(MOD_2) {
        init(v);
    }

    RollingHash(string& s, ll B_1 = 1007, ll B_2 = 1009, ll MOD_1 = 1000000007,
                ll MOD_2 = 1000000009)
        : B_1(B_1), B_2(B_2), MOD_1(MOD_1), MOD_2(MOD_2) {
        vector<T> v;
        for (char c : s) v.emplace_back(c);
        init(v);
    }

    void init(vector<T> v) {
        int n = v.size();
        hash_1.assign(n + 1, 0);
        pow_1.assign(n + 1, 1);
        for (int i = 0; i < n; i++) {
            hash_1[i + 1] = (hash_1[i] * B_1 + v[i]) % MOD_1;
            pow_1[i + 1] = pow_1[i] * B_1 % MOD_1;
        }
    }

    ll get(int l, int r) {
        ll res_1 =
            ((hash_1[r] - hash_1[l] * pow_1[r - l]) % MOD_1 + MOD_1) % MOD_1;
        return res_1;
    }
};

signed main() {
    int n;
    cin >> n;
    string s[n];
    rep(i, n) {
        cin >> s[i];
    }
    map<ll, int> cnt[26];
    rep(i, n) {
        RollingHash<char> rh(s[i]);
        set<int> S;
        S.emplace(s[i][0] - 'a');
        int m = s[i].size();
        for (int j = 1; j <= m; j++) {
            for(int s : S){
                cnt[s][rh.get(j, m)]++;
            }
            if (j == m) continue;
            S.emplace(s[i][j] - 'a');
        }
    }
    ll ans = 0;
    rep(i, n) {
        RollingHash<char> rh(s[i]);
        ans += cnt[s[i][0] - 'a'][rh.get(1, (int)s[i].size())];
    }
    cout << ans - n << endl;
    return 0;
}