output_description: Print the number of unordered pairs (S_i, S_j) where i \neq j and Limak can
  obtain one string from the other.
  
  * * *
input_description: Input is given from Standard Input in the following format.
  
      
      
      N
      S_1
      S_2
      \vdots
      S_N
attempt: import sys
  
  read = sys.stdin.read
  readline = sys.stdin.readline
  readlines = sys.stdin.readlines
  
  n = int(readline())
  s = read().split()
  s.sort(key=lambda x: len(x))
  
  words = dict()
  # for si in 'abcdefghijklmnopqrstuvmxyz':
  #     words[si] = dict()
  
  ans = 0
  for si in s:
      if len(si) == 1:
          head = si[0]
          tail = ""
      else:
          head = si[0]
          tail = si[1:]
  
      heads = set()
      heads.add(head)
  
      now = ""
      stack = [""]
      for ti in tail[::-1]:
          now += ti
          stack.append(now)
  
      for ti in stack[::-1]:
          if ti in words:
              for j in heads:
                  if j in words[ti]:
                      ans += words[ti][j]
          if ti == "":
              break
          heads.add(ti[-1])
  
      tail = stack[-1]
  
      if not tail in words:
          words[tail] = dict()
      if not head in words[tail]:
          words[tail][head] = 1
      else:
          words[tail][head] += 1
  
      # print(si,ans)
      # print(stack)
  
  print(ans)
  
problem_description: 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?
samples: [{"input": "3\n    abcxyx\n    cyx\n    abc", "output": "1\n    \n\nThe only good pair is (abcxyx, cyx).\n\n* * *"}, {"input": "6\n    b\n    a\n    abc\n    c\n    d\n    ab", "output": "5\n    \n\nThere are five good pairs: (b, abc), (a, abc), (abc, c), (b, ab), (a, ab)."}]