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++ (Clang 10.0.0) (judge verdict: Accepted)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

const int N = 200200;
const int M = (int)1e6 + 7;
const int A = 26;
string s[N];
int n;
pii ord[N];
int go[M][A];
int m;
int a[M][A];
int cnt[A];
ll ans = 0;

void solve(int id) {
	int v = 0;
	int L = (int)s[id].length();
	for (int i = 0; i < A; i++)
		cnt[i] = 0;
	for (int i = 0; i < L; i++)
		cnt[(int)(s[id][i] - 'a')]++;
	for (int i = 0; i < L; i++) {
		for (int c = 0; c < A; c++)
			if (cnt[c] > 0)
				ans += a[v][c];
		if (i == L - 1) break;
		int c = (int)(s[id][i] - 'a');
		cnt[c]--;
		if (go[v][c] == 0) go[v][c] = m++;
		v = go[v][c];
	}
	int c = (int)(s[id].back() - 'a');
	a[v][c]++;
}

int main()
{
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		reverse(all(s[i]));
		ord[i] = mp((int)s[i].length(), i);
	}
	sort(ord, ord + n);
	m = 1;
	for (int i = 0; i < n; i++)
		solve(ord[i].second);
	cout << ans << "\n";

	return 0;
}