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)

#include <bits/stdc++.h>
using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) static_cast<int>((x).size())

template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
  template<class... Args>
  vec(size_t n = 0, Args... args)
      : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
  template<class... Args>
  vec(Args... args)
      : vector<T>(args...) {}
};

template<class T>
inline bool Minimize(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool Maximize(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int Next(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int Prev(int i, int n) { return !i ? n - 1 : i - 1; }

mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));

constexpr int kMod1 = 1e9 + 9, kMod2 = 1e9 + 7;

int Mod = kMod1;
struct ModInt {
  int a;
  ModInt(int t_a = 0) {
    a = (t_a < Mod ? (t_a < 0 ? t_a + Mod : t_a) : t_a - Mod);
  }
  friend istream& operator>>(istream& in_stream, ModInt& o) {
    in_stream >> o.a;
    o.a = (o.a < Mod ? (o.a < 0 ? o.a + Mod : o.a) : o.a - Mod);
    return in_stream;
  }
  friend ostream& operator<<(ostream& out_stream, const ModInt& o) {
    out_stream << o.a;
    return out_stream;
  }

  bool operator!() const { return !a; }
  explicit operator bool() const { return !!a; }

  ModInt operator-() const { return ModInt() - *this; }
  ModInt operator+(const ModInt& o) const { return ModInt(a + o.a); }
  ModInt operator-(const ModInt& o) const { return ModInt(a - o.a); }
  ModInt operator*(const ModInt& o) const { return ModInt(static_cast<int>(static_cast<int64_t>(a) * o.a % Mod)); }
  ModInt operator/(const ModInt& o) const { return *this * o.Inverse(); }
  ModInt& operator+=(const ModInt& o) { return *this = *this + o; }
  ModInt& operator-=(const ModInt& o) { return *this = *this - o; }
  ModInt& operator*=(const ModInt& o) { return *this = *this * o; }
  ModInt& operator/=(const ModInt& o) { return *this = *this / o; }
  ModInt& operator++() { return *this = *this + ModInt(1); }
  ModInt& operator--() { return *this = *this - ModInt(1); }
  ModInt Inverse() const { return Power(Mod - 2); }
  template<class T>
  ModInt Power(T exp) const {
    ModInt ret(1), c = *this;
    for (; exp; exp >>= 1, c *= c) {
      if (exp & 1) {
        ret *= c;
      }
    }
    return ret;
  }
};
ModInt base(311);

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  int n_strings; cin >> n_strings;
  vec<string, 1> strings(n_strings);
  for (auto& i : strings) {
    cin >> i;
  }

  sort(ALL(strings), [&](const auto& i, const auto& j) { return SZ(i) < SZ(j); });

  int64_t answer = 0;
  map<pair<int, int>, set<char>> f;
  for (auto& i : strings) {
    vec<int, 1> first_index_characters(26);
    for (int j = 0; j < 26; ++j) {
      first_index_characters[j] = static_cast<int>(find(ALL(i), static_cast<char>(j + 'a')) - i.begin());
    }

    ModInt value_1, value_2;
    for (int j = SZ(i); j; --j) {
      for (int k = 0; k < 26; ++k) {
        answer += first_index_characters[k] < j && f[make_pair(value_1.a, value_2.a)].count(static_cast<char>(k + 'a'));
      }
      if (j - 1) {
        value_1 = value_1 * base + ModInt(i[j - 1]);
        Mod = kMod2;
        value_2 = value_2 * base + ModInt(i[j - 1]);
        Mod = kMod1;
      }
    }
    f[make_pair(value_1.a, value_2.a)].emplace(i[0]);
  }

  cout << answer << '\n';

  return 0;
}