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: Runtime Error)

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

class str_len{
  public:
  bool operator () ( pair<string,int>& a , pair<string,int>& b ){ return a.first.length() < b.first.length();}
};

class BIT{
  public:
  vector<vector<int>> s;
  const int n;
  BIT (int n_):s(n_+1,vector<int>(26,0)),n(n_){}
  //[0,i],1-index
  int sum(int i,char c){
    int k = 0;
    while(i > 0){
      k += s[i][c-'a'];
      i -= i & (-i);
    }
    return k;
  }
  //[l,r],1-index
  int sum(int l,int r,char c,bool aa){
    return sum(n,c)-sum(l-1,c);
  }
  // 1-index
  void add(int pos,int val,char c){
    while(pos <= n){
      s[pos][c-'a'] += val;
      pos += pos & (-pos);
    }
  }
};

int bs(string &s,vector<string> &v){
  int l = -1,r = v.size()-1;
  while(r-l > 1){
    int mid = (r+l)/2;
    if(v[mid] < s)l = mid;
    else r = mid;
  }
  return r;
}

int main(){
  
  int n;
  cin >> n;
  vector<pair<string,int>> S(n);
  vector<vector<string>> cnt(1000000);
  vector<int> one(26,0);
  vector<int> howmany(26,0);
  int mx = 0;
  vector<BIT> bit;
  rep(i,n){
    cin >> S[i].first;
    S[i].second = i;
    bit.push_back(S[i].first.size());
    reverse(ALL(S[i].first));
    vector<int> v(26,0);
    for(int j = 1;j <= S[i].first.size();j++)bit[i].add(j,1,S[i].first[j-1]),v[S[i].first[j-1]-'a']++;
    if(S[i].first.size() == 1){
      howmany[S[i].first[0]-'a']++;
    }
    string t;
    t = "";
    if(S[i].first.size() != 1)rep(au,26)if(v[au] > 0)one[au]++;
    int len = S[i].first.size();
    mx = max(mx,len-2);
    rep(j,len-2){
      t += S[i].first[j];
      v[S[i].first[j]-'a']--;
      cnt[j+1].push_back(t);
    }
  }
  sort(ALL(S),str_len());
  rep(i,mx+1)sort(ALL(cnt[i]));
  ll res = 0;
  rep(i,26)res += 1ll*one[i]*howmany[i];
  rep(i,n){
    if(S[i].first.size() == 1)continue;
    char c = S[i].first.back();
    S[i].first.pop_back();
    int len = cnt[S[i].first.size()].size();
    for(int j = bs(S[i].first,cnt[S[i].first.size()]);j >= 0 && j < len && cnt[S[i].first.size()][j] == S[i].first;j++){
      if(bit[S[j].second].sum(S[i].first.size()+1,S[j].first.size(),c,true) > 0)res++;
    }
  }
  cout << res << "\n";




  
  

  return 0;
}