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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func check(a, b string) bool {
	if a == b {
		return true
	}
	i := 0
	for i < len(a) {
		if a[i] == b[0] {
			break
		}
		i++
	}
	if len(a) == i {
		return false
	}
	a = a[i:]
	d := len(a) - len(b)
	if d > 0 {
		a = string(a[0]) + a[d+1:]
	}
	return a == b
}

func rev(s []byte) string {
	N := len(s)
	for i := 0; i < N/2; i++ {
		s[i], s[N-1-i] = s[N-1-i], s[i]
	}
	return string(s)
}

func check2(a, b string) bool {
	if len(a) > len(b) {
		return false
	}
	for i := 0; i < len(a)-1; i++ {
		if a[i] != b[i] {
			return false
		}
	}
	for i := len(a) - 1; i < len(b); i++ {
		if a[len(a)-1] == b[i] {
			return true
		}
	}
	return false
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	N := getInt()
	s := make([]string, N)
	for i := 0; i < N; i++ {
		s[i] = rev([]byte(getString()))
	}
	sort.Strings(s)
	// out(s)
	cnt := 0
	for i := 0; i < len(s); i++ {
		// a := s[i][:len(s[i])-1]
		for j := i + 1; j < len(s); j++ {
			if len(s[i]) > len(s[j]) {
				break
			}
			if check2(s[i], s[j]) {
				cnt++
			}
		}
	}
	out(cnt)
}