Problem p02962 (python)

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

    S = input().rstrip('\n')
    T = input().rstrip('\n')

    ns = len(S)
    nt = len(T)

    mod1 = 1000000007
    mod2 = 1000000009
    mod3 = 998244353
    b1 = 1007
    b2 = 2009
    b3 = 2999
    H1 = 0
    H2 = 0
    H3 = 0
    for i, t in enumerate(T):
        t = ord(t)
        H1 = ((H1 * b1)%mod1 + t)%mod1
        H2 = ((H2 * b2)%mod2 + t)%mod2
        H3 = ((H3 * b3)%mod3 + t)%mod3
    h1 = 0
    h2 = 0
    h3 = 0
    for i in range(nt):
        s = ord(S[i%ns])
        h1 = ((h1 * b1) % mod1 + s) % mod1
        h2 = ((h2 * b2) % mod2 + s) % mod2
        h3 = ((h3 * b3) % mod3 + s) % mod3

    idx = []
    if h1 == H1 and h2 == H2 and h3 == H3:
        idx.append(0)
    b1_nt = pow(b1, nt, mod1)
    b2_nt = pow(b2, nt, mod2)
    b3_nt = pow(b3, nt, mod3)
    for i in range(1, ns):
        s = ord(S[(i+nt-1)%ns])
        s_prev = ord(S[i-1])
        h1 = ((h1 * b1) % mod1 - (b1_nt * s_prev)%mod1 + s) % mod1
        h2 = ((h2 * b2) % mod2 - (b2_nt * s_prev)%mod2 + s) % mod2
        h3 = ((h3 * b3) % mod3 - (b3_nt * s_prev) % mod3 + s) % mod3
        if h1 == H1 and h2 == H2 and h3 == H3:
            idx.append(i%ns)
    if len(idx) == 0:
        print(0)
        exit()
    ans = 1
    dic = {}
    for i in range(len(idx)):
        if (idx[i] - nt)%ns in dic:
            tmp = dic[(idx[i]-nt)%ns] + 1
            dic[idx[i]] = tmp
            ans = max(ans, tmp)
        else:
            dic[idx[i]] = 1
    for i in range(len(idx)):
        if (idx[i] - nt)%ns in dic:
            tmp = dic[(idx[i]-nt)%ns] + 1
            dic[idx[i]] = tmp
            ans = max(ans, tmp)
        else:
            dic[idx[i]] = 1
    ans_ = ans
    for i in range(len(idx)):
        if (idx[i] - nt)%ns in dic:
            tmp = dic[(idx[i]-nt)%ns] + 1
            dic[idx[i]] = tmp
            ans = max(ans, tmp)
        else:
            dic[idx[i]] = 1
    if ans == ans_:
        print(ans)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline

    S = input().rstrip('\n')
    T = input().rstrip('\n')

    ns = len(S)
    nt = len(T)
    S = ''.join([S] * ((nt-1)//ns+1))
    S = ''.join([S, S, S, S])
    ns = len(S)

    mod1 = 1000000007
    mod2 = 1000000009
    mod3 = 998244353
    b1 = 1007
    b2 = 2009
    b3 = 2999
    H1 = 0
    H2 = 0
    H3 = 0
    for i, t in enumerate(T):
        t = ord(t)
        H1 = ((H1 * b1)%mod1 + t)%mod1
        H2 = ((H2 * b2)%mod2 + t)%mod2
        H3 = ((H3 * b3)%mod3 + t)%mod3
    h1 = 0
    h2 = 0
    h3 = 0
    for i in range(nt):
        s = ord(S[i])
        h1 = ((h1 * b1) % mod1 + s) % mod1
        h2 = ((h2 * b2) % mod2 + s) % mod2
        h3 = ((h3 * b3) % mod3 + s) % mod3

    idx = [-10**10]
    if h1 == H1 and h2 == H2 and h3 == H3:
        idx.append(0)
    b1_nt = pow(b1, nt, mod1)
    b2_nt = pow(b2, nt, mod2)
    b3_nt = pow(b3, nt, mod3)
    for i in range(nt, ns):
        s = ord(S[i])
        s_prev = ord(S[i-nt])
        h1 = ((h1 * b1) % mod1 - (b1_nt * s_prev)%mod1 + s) % mod1
        h2 = ((h2 * b2) % mod2 - (b2_nt * s_prev)%mod2 + s) % mod2
        h3 = ((h3 * b3) % mod3 - (b3_nt * s_prev) % mod3 + s) % mod3
        if h1 == H1 and h2 == H2 and h3 == H3:
            if i-nt+1 - idx[-1] >= nt:
                idx.append(i-nt+1)
    if len(idx) == 1:
        print(0)
        exit()
    tmp = 1
    ans = 1
    ans_ = 1
    ns_ = ns // 2
    for i in range(1, len(idx)):
        if idx[i] - idx[i-1] == nt:
            tmp += 1
            if idx[i] < ns_:
                ans = max(ans, tmp)
                ans_ = max(ans_, tmp)
            else:
                ans_ = max(ans_, tmp)
        else:
            tmp = 1
    if ans_ == ans:
        print(ans)
    else:
        print(-1)
        assert 0


if __name__ == '__main__':
    main()