import kotlin.math.min val MOD = 1_000_000_007 fun main() { val (N) = readInts() val S = Array(N){ readLine()!!} val lcps = mutableListOf>() for (i in 0 until N - 1) { var l = 0 val mx = min(S[i].length, S[i + 1].length) while(l < mx && S[i][l] == S[i + 1][l]) l++ lcps.add(Pair(i, l)) } lcps.sortBy { it.second } val t = sortedSetOf() t.add(-1) t.add(N - 1) var ans = 0L S.forEach { ans += it.length } for (lcp in lcps) { val i = lcp.first val l = (i - t.lower(i)!! - 1).toLong() val r = (t.higher(i)!! - i - 1).toLong() var lensum = (l + 1) * (r + 1) * 2 lensum += (l + 1) * r * (r + 1) / 2 lensum += l * (l + 1) / 2 * (r + 1) ans += lensum * lcp.second debug{"l:$l r:$r"} t.add(i) } println(ans) } private val isDebug = try { // なんか本番でエラーでる System.getenv("MY_DEBUG") != null } catch (t: Throwable) { false } private fun readLn() = readLine()!! private fun readInt() = readLn().toInt() private fun readStrings() = readLn().split(" ") private fun readInts() = readStrings().map { it.toInt() }.toIntArray() private fun readLongs() = readStrings().map { it.toLong() }.toLongArray() private inline fun debug(msg: () -> String) { if (isDebug) System.err.println(msg()) } private fun debug(a: LongArray) { debug{a.joinToString(" ")} } private fun debug(a: IntArray) { debug{a.joinToString(" ")} } private fun debug(a: BooleanArray) { debug{a.map{if(it) 1 else 0}.joinToString("")} } private fun debugDim(A: Array) { if (isDebug) { for (a in A) { debug(a) } } }