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)

fn main() {
    // 問題文中で使われる変数の型宣言をしておく。
    let n: i64;
    let mut s: Vec<String>;
    // 【入力段ここから】
    // 定型句 bufにstdinから1行読み込む。読み込んだ後、CRLFが入りっぱなしになっている点に注意。
    let mut buf: String;
    buf = String::new();
    let stdin_obj = std::io::stdin();
    stdin_obj.read_line(&mut buf).ok();
    // 定型句 buf（Str）に読み込んだ文字列をi64にパースしてnに代入。
    n = buf.trim().parse().ok().unwrap();
    s = Vec::<String>::with_capacity(n as usize);
    for _ in 0..n {
        buf = String::new();
        stdin_obj.read_line(&mut buf).ok();
        s.push(buf.trim().to_string());
    }
    // 【入力段ここまで】
    // ==================================
    // 【計算段ここから】
    s.sort_by_key(|a| a.len());
    let mut counter = 0;
    for i in 0..n {
        let s_i = s[i as usize].as_bytes(); // 削る目標
        for j in (i + 1)..n {
            let s_j = s[j as usize].as_bytes(); // 削る対象
            let mut tgt_ix: i32 = (s_i.len() as i32) - 1;
            // ケース1 削る目標が1文字の場合、削る対象の中にその1文字が入っていたら、OK判定。
            if s_i.len() == 1 {
                for x in 0..s_j.len() {
                    if s_i[0] == s_j[x] {
                        counter += 1;
                        break;
                    }
                }
                continue;
            }
            // ケース2 末尾が違った場合、ケース1でOKになってなかったら、NG判定して弾く。
            if s_j[(s_j.len() - 1) as usize] != s_i[(s_i.len() - 1) as usize] {
                continue;
            }
            // ケース3 ケース1、ケース2に入らなかった場合、後ろから見ていって残機1が消費し切る前に一致させられるか判定。
            let mut remaining: i32 = 1; // 残機
            let mut now_ok = true; // これがfalse→trueに変わるタイミングで残機1消費。
            for x in 0..s_j.len() {
                let del_ix = s_j.len() - (x + 1);
                if s_j[del_ix as usize] == s_i[tgt_ix as usize] {
                    tgt_ix -= 1;
                    if !now_ok {
                        now_ok = true;
                        remaining -= 1;
                    }
                    if tgt_ix < 0 {
                        break;
                    }
                } else {
                    if now_ok {
                        now_ok = false;
                        if tgt_ix != 0 {
                            remaining = -1;
                            break;
                        }
                    }
                }
            }
            if tgt_ix < 0 && remaining >= 0 {
                counter += 1;
            }
        }
    }
    println!("{}", counter);
}