Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=lambda x:-len(x))
P = 2**61 - 1

from collections import defaultdict,Counter
D = defaultdict(lambda: Counter())

ans = 0
for s in S:
    sets = [set()]
    for c in s[::-1]:
        i = ord(c) - ord('a')
        sets.append(sets[-1] | {i})
    sets.reverse()
    nxs = []
    a = 0
    for l,(c,st) in enumerate(zip(s,sets)):
        for j in st:
            na = (a*27 + (j+1)) % P
            nxs.append((na,l))
        i = ord(c) - ord('a') + 1
        a *= 27
        a += i
        a %= P
    if a in D[len(s)-1]:
        ans += D[len(s)-1][a]
    for na,l in nxs:
        D[l][na] += 1

print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=len)
P = 2**61 - 1

from collections import defaultdict
D = defaultdict(lambda: [0]*26)

ans = 0
for s in S:
    l = ord(s[-1]) - ord('a')
    a = 0
    tmp = [0] * 26
    for c in s[:-1]:
        i = ord(c) - ord('a')
        if a in D:
            for j in range(26):
                tmp[j] += D[a][j]
        ans += tmp[i]
        tmp[i] = 0
        a = (a*27 + i + 1) % P
    ans += tmp[l]
    D[a][l] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=lambda x:-len(x))
P = 2**61 - 1

from collections import defaultdict,Counter
D = defaultdict(lambda: Counter())

ans = 0
for s in S:
    sets = [0]
    for c in s[::-1]:
        i = ord(c) - ord('a')
        sets.append(sets[-1] | (1<<i))
    sets.reverse()
    nxs = []
    a = 0
    for l,(c,st) in enumerate(zip(s,sets)):
        for j in range(26):
            if st&(1<<j):
                na = (a*27 + (j+1)) % P
                nxs.append((na,l))
        i = ord(c) - ord('a') + 1
        a *= 27
        a += i
        a %= P
    if a in D[len(s)-1]:
        ans += D[len(s)-1][a]
    for na,l in nxs:
        D[l][na] += 1

print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=len)
P = 2**61 - 1

from collections import defaultdict
D = defaultdict(lambda: [0]*26)

ans = 0
for s in S:
    l = ord(s[-1]) - ord('a')
    a = 0
    tmp = [0] * 26
    for c in s[:-1]:
        i = ord(c) - ord('a')
        if a in D:
            for j in range(26):
                tmp[j] += D[a][j]
        ans += tmp[i]
        tmp[i] = 0
        a = (a*27 + i + 1) % P
    ans += tmp[l]
    D[a][l] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=lambda x:-len(x))
P = 2**61 - 1

from collections import defaultdict,Counter
D = defaultdict(lambda: Counter())

ans = 0
for s in S:
    a = 0
    for c in s:
        i = ord(c) - ord('a') + 1
        a *= 27
        a += i
        a %= P
    if a in D[len(s)-1]:
        ans += D[len(s)-1][a]
    sets = [0]
    for c in s[::-1]:
        i = ord(c) - ord('a')
        sets.append(sets[-1] | (1<<i))
    sets.reverse()
    a = 0
    for l,(c,st) in enumerate(zip(s,sets)):
        for j in range(26):
            if st&(1<<j):
                na = (a*27 + (j+1)) % P
                D[l][na] += 1
        i = ord(c) - ord('a') + 1
        a *= 27
        a += i
        a %= P

print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=len)
P = 2**61 - 1

from collections import defaultdict
D = defaultdict(lambda: [0]*26)

ans = 0
for s in S:
    l = ord(s[-1]) - ord('a')
    a = 0
    tmp = [0] * 26
    for c in s[:-1]:
        i = ord(c) - ord('a')
        if a in D:
            for j in range(26):
                tmp[j] += D[a][j]
        ans += tmp[i]
        tmp[i] = 0
        a = (a*27 + i + 1) % P
    ans += tmp[l]
    D[a][l] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    N = int(input())
    S = [input().rstrip() for i in range(N)]

    from collections import defaultdict
    T = defaultdict(lambda: set())
    for s in S:
        T[len(s)].add(s)

    ans = 0
    for s in S:
        pre = set([s[0]])
        for l in range(len(s)-1, 0, -1):
            pre.add(s[-l])
            if T[l]:
                for p in pre:
                    suf = '' if l==1 else s[-l+1:]
                    if p+suf in T[l]:
                        ans += 1
    print(ans)
solve()

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=len)
P = 2**61 - 1

from collections import defaultdict
D = defaultdict(lambda: [0]*26)

ans = 0
for s in S:
    l = ord(s[-1]) - ord('a')
    a = 0
    tmp = [0] * 26
    for c in s[:-1]:
        i = ord(c) - ord('a')
        if a in D:
            for j in range(26):
                tmp[j] += D[a][j]
        ans += tmp[i]
        tmp[i] = 0
        a = (a*27 + i + 1) % P
    ans += tmp[l]
    D[a][l] += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    N = int(input())
    S = [input().rstrip() for i in range(N)]

    from collections import defaultdict
    T = defaultdict(lambda: set())
    for s in S:
        T[len(s)].add(s)

    ans = 0
    for s in S:
        pre = set([s[0]])
        for l in range(len(s)-1, 0, -1):
            pre.add(s[-l])
            if T[l]:
                for p in pre:
                    suf = '' if l==1 else s[-l+1:]
                    if p+suf in T[l]:
                        ans += 1
    print(ans)
solve()

Fixed submission:
import sys
input = sys.stdin.readline

N = int(input())
S = [input().rstrip()[::-1] for i in range(N)]
S.sort(key=len)
P = 2**61 - 1

from collections import defaultdict
D = defaultdict(lambda: [0]*26)

ans = 0
for s in S:
    l = ord(s[-1]) - ord('a')
    a = 0
    tmp = [0] * 26
    for c in s[:-1]:
        i = ord(c) - ord('a')
        if a in D:
            for j in range(26):
                tmp[j] += D[a][j]
        ans += tmp[i]
        tmp[i] = 0
        a = (a*27 + i + 1) % P
    ans += tmp[l]
    D[a][l] += 1

print(ans)