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 Rust (1.42.0) (judge verdict: Time Limit Exceeded)

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

#[derive(Debug)]
struct TrieNode {
    ch: char,
    count: usize,
    children: HashMap<char, TrieNode>,
}

impl TrieNode {
    fn with_char(ch: char) -> TrieNode {
        TrieNode {
            ch: ch,
            count: 0,
            children: HashMap::new(),
        }
    }
}

#[derive(Debug)]
struct Trie {
    node: TrieNode,
}

impl Trie {
    fn new() -> Trie {
        let node = TrieNode::with_char('#');
        Trie { node: node }
    }

    fn add_word(&mut self, word: &[char]) {
        let mut cur = &mut self.node;
        for &ch in word {
            if !cur.children.contains_key(&ch) {
                let node = TrieNode::with_char(ch);
                cur.children.insert(ch, node);
            }
            cur = cur.children.get_mut(&ch).unwrap();
        }
        cur.count += 1;
    }

    fn count(&self, word: &[char]) -> usize {
        let mut cur = &self.node;
        for &ch in word {
            if !cur.children.contains_key(&ch) {
                return 0;
            }
            cur = cur.children.get(&ch).unwrap();
        }
        cur.count
    }
}

fn main() {
    let n = read::<usize>();
    let mut s = vec![];
    for i in 0..n {
        let mut ss = read::<String>().chars().collect::<Vec<_>>();
        ss.reverse();
        s.push(ss);
    }
    s.sort_by_key(|x| x.len());
    let mut trie = Trie::new();
    for word in &s {
        trie.add_word(&word);
    }
    // debug!(trie);

    let exist_length = s.iter().map(|x| x.len()).collect::<HashSet<_>>();

    let mut ans = 0i64;
    for word in &s {
        let mut count = HashMap::new();
        for ch in word {
            *count.entry(ch).or_insert(0) += 1;
        }
        let mut cur_key = vec![];
        for l in 0..word.len() {
            if exist_length.contains(&(l + 1)) {
                for ch in b'a'..b'z' + 1 {
                    let ch = ch as char;
                    if !count.contains_key(&ch) || count[&ch] == 0 {
                        continue;
                    }
                    cur_key.push(ch);
                    ans += trie.count(&cur_key) as i64;
                    // debug!(cur_key, trie.count(&cur_key));
                    cur_key.pop();
                }
            }
            cur_key.push(word[l]);
            *count.get_mut(&word[l]).unwrap() -= 1;
        }
    }
    println!("{}", ans - s.len() as i64);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}