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

#![allow(unused_imports)]
#![allow(non_snake_case, unused)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;

// https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254 より

macro_rules! eprint {
    ($($t:tt)*) => {{
        use ::std::io::Write;
        let _ = write!(::std::io::stderr(), $($t)*);
    }};
}
macro_rules! eprintln {
    () => { eprintln!(""); };
    ($($t:tt)*) => {{
        use ::std::io::Write;
        let _ = writeln!(::std::io::stderr(), $($t)*);
    }};
}
macro_rules! dbg {
    ($v:expr) => {{
        let val = $v;
        eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
        val
    }}
}

macro_rules! mat {
    ($($e:expr),*) => { Vec::from(vec![$($e),*]) };
    ($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
    ($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
    ($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

macro_rules! ok {
    ($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
        $a$([$i])*.$f($($t),*)
    };
    ($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
        let t = $e;
        ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
    } };
}

pub fn readln() -> String {
    let mut line = String::new();
    ::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
    line
}

macro_rules! read {
    ($($t:tt),*; $n:expr) => {{
        let stdin = ::std::io::stdin();
        let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
            let line = line.unwrap();
            let mut it = line.split_whitespace();
            _read!(it; $($t),*)
        }).collect::<Vec<_>>();
        ret
    }};
    ($($t:tt),*) => {{
        let line = readln();
        let mut it = line.split_whitespace();
        _read!(it; $($t),*)
    }};
}

macro_rules! _read {
    ($it:ident; [char]) => {
        _read!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; [u8]) => {
        Vec::from(_read!($it; String).into_bytes())
    };
    ($it:ident; usize1) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:tt),+) => {
        ($(_read!($it; $t)),*)
    };
}


pub fn main() {
    let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

// const MOD: i64 = 998244353;
const MOD: usize = 1_000_000_007;
const INF: i64 = std::i64::MAX/2;

use std::cell::RefCell;
use std::rc::Rc;

type Ref = Rc<RefCell<Node>>;
type Link = Option<Ref>;

pub struct Node {
    v: Vec<Link>,
    count: Vec<i64>,
}

impl Node {
    pub fn new() -> Self{
        Node {
            v: vec![None;26],
            count: vec![0;26],
        }
    }
}

fn add(mut node: Link, c: &[char]) {
    let n = c.len();
    let mut count = vec![0;26];
    add_dfs(node, &mut count, &c, 0);
}

fn add_dfs(mut node: Link, mut count: &mut Vec<i64>, c: &[char], idx: usize) {
    if node.is_none() {
        return;
    }
    if idx==c.len() {
        return;
    }
    let cur = node.unwrap();
    let nxt = (c[idx] as u8 - 'a' as u8) as usize;
    if cur.borrow().v[nxt].is_none() {
        cur.borrow_mut().v[nxt] = Some(Rc::new(RefCell::new(Node::new())));
    }
    add_dfs(cur.borrow().v[nxt].clone(),&mut count, &c, idx+1);
    count[nxt] = 1;
    for i in 0..26 {
        cur.borrow_mut().count[i] += count[i];
    }
}

fn find(mut node: Link, c: &[char]) -> i64 {
    let n = c.len();
    let mut count = 0;
    find_dfs(node, &mut count, &c, 0);
    return count;
}

fn find_dfs(mut node: Link, mut count: &mut i64, c: &[char], idx: usize) -> bool {
    if node.is_none() {
        return false;
    }
    let cur = node.unwrap();
    let nxt = (c[idx] as u8 - 'a' as u8) as usize;
    if idx==c.len()-1 {
        *count = cur.borrow().count[nxt];
        return true;
    }
    if cur.borrow().v[nxt].is_none() {
        return false;
    }
    return find_dfs(cur.borrow().v[nxt].clone(),&mut count, &c, idx+1);
}

fn solve() {
    let n = read!(usize);
    let mut c = read!([char];n);
    let mut v = vec![];
    for c in &c {
        v.push((c.len(),c));
    }
    v.sort_by(|a,b| b.cmp(a));
    let mut root = Some(Rc::new(RefCell::new(Node::new())));
    let mut ans = 0;
    for (_,x) in v {
        let mut c = x.clone();
        c.reverse(); 
        let tmp = find(root.clone(),&c);
        // println!("{:?} {}",c, tmp);
        // println!("{:?}", root.clone().unwrap().borrow().count);
        ans += tmp;
        add(root.clone(), &c);
    }
    println!("{}",ans);
}

fn run() {
    solve();
}