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

/*
{

AuThOr Gwj
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n=200000+20;
string s[200000+20];
int son[1000000+1][27],cnt[1000000+1][27],old[1000000+1][27];
int root=0;
int cnt_=0;
int have[27];
void go(int now,int is,int t){
	if(is==s[t].length()) return;
	if(son[now][s[t][is]]){
	if(have[s[t][is]]==1){
		old[son[now][s[t][is]]][s[t][is]]++;
		cnt[son[now][s[t][is]]][s[t][is]]++;
	}
	have[s[t][is]]--;
		go(son[now][s[t][is]],is+1,t);
	}
	else{
		son[now][s[t][is]]=++cnt_;
	if(have[s[t][is]]==1){
		old[son[now][s[t][is]]][s[t][is]]++;
		cnt[son[now][s[t][is]]][s[t][is]]++;
	}
	have[s[t][is]]--;
		go(son[now][s[t][is]],is+1,t);
	}	
}
void calc(int now){
	rb(i,1,26){
		if(son[now][i]){
			calc(son[now][i]);
			rb(k,1,26)
				cnt[now][k]+=cnt[son[now][i]][k];
		}
	}
}
int rest(int now,int is,string& s_,int need){	
	if(is==s_.length()) return cnt[now][need]-old[now][need]-1;
	return rest(son[now][s_[is]],is+1,s_,need);
}
int main(){
	fastio;
	R(n);
	rb(i,1,n){
		rb(j,1,26)
			have[j]=0;
		R(s[i]);
		reverse(ALL(s[i]));
		rep(j,s[i].length()){
			s[i][j]-='a'-1;
		}
		rb(j,0,s[i].length()-1)
			have[s[i][j]]++;
		go(root,0,i);
	}	
	calc(root);
	LL res=0;
	rb(i,1,n){
		string ss="";
		rep(j,s[i].length()){
			if(j!=s[i].length()-1) ss+=s[i][j];
		}
		res+=rest(root,0,ss,s[i][s[i].length()-1]);	
	}
	cout<<res<<endl;
	return 0;
}