Problem p03096 (python)

Fixed submission:
import sys
import math
import collections
import bisect
import copy

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    last = [-1 for _ in range(2 * 10 ** 5 + 1)]
    dp = [INF for _ in range(n + 1)]
    dp[0] = 1

    d = [ni()-1 for _ in range(n)]

    for i, di in enumerate(d):
        dp[i + 1] = dp[i]
        if last[di] != -1:
            if i - last[di] != 1:
                dp[i + 1] += dp[last[di] + 1]
                dp[i + 1] %= MOD
        last[di] = i

    print(dp[n])


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import math
import collections
import bisect
import copy

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(input())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    idxlist = [[] for _ in range(n)]
    d = []
    dp = [INF for _ in range(n + 1)]
    dp[0] = 1

    for i in range(n):
        di = ni()
        di -= 1
        if i > 0:
            if d[-1] == di:
                d.append(-1)
                continue
        d.append(di)
        idxlist[di].append(i)

    for i, di in enumerate(d):
        dp[i + 1] = dp[i]
        if di == -1:
            continue
        bidx = bisect.bisect_left(idxlist[di], i)
        if bidx - 1 >= 0:
            dp[i + 1] += dp[idxlist[di][bidx - 1] + 1]
            dp[i + 1] %= MOD

    print(dp[n])


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import math
import collections
import bisect
import copy

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    idxlist = [[] for _ in range(n)]
    d = []
    dp = [INF for _ in range(n + 1)]
    dp[0] = 1

    for i in range(n):
        di = ni()
        di -= 1
        if i > 0:
            if d[-1] == di:
                d.append(-1)
                continue
        d.append(di)
        idxlist[di].append(i)

    for i, di in enumerate(d):
        dp[i + 1] = dp[i]
        if di == -1:
            continue
        bidx = bisect.bisect_left(idxlist[di], i)
        if bidx - 1 >= 0:
            dp[i + 1] += dp[idxlist[di][bidx - 1] + 1]
            dp[i + 1] %= MOD

    print(dp[n])


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import math
import collections
import bisect
import copy

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    idxlist = [[] for _ in range(n)]
    d = []
    dp = [INF for _ in range(n + 1)]
    dp[0] = 1

    for i in range(n):
        tmp = ni()
        tmp -= 1
        if i > 0 and d[-1] == tmp:
            d.append(-1)
            continue
        d.append(tmp)
        idxlist[tmp].append(i)

    for i, di in enumerate(d):
        dp[i + 1] = dp[i]
        if di == -1:
            continue
        bidx = bisect.bisect_left(idxlist[di], i)
        if bidx - 1 >= 0:
            dp[i + 1] += dp[idxlist[di][bidx - 1] + 1]
            dp[i + 1] %= MOD

    print(dp[n])


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
import math
import collections
import bisect
import copy

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    idxlist = [[] for _ in range(n)]
    d = []
    dp = [INF for _ in range(n + 1)]
    dp[0] = 1

    prev = -1
    for i in range(n):
        tmp = ni()
        tmp -= 1
        if tmp == prev:
            d.append(-1)
            continue
        d.append(tmp)
        idxlist[tmp].append(i)
        prev = tmp

    for i, di in enumerate(d):
        dp[i + 1] = dp[i]

        if di == -1:
            continue
        bidx = bisect.bisect_left(idxlist[di], i)
        if bidx - 1 >= 0:
            dp[i + 1] += dp[idxlist[di][bidx - 1] + 1]
            dp[i] %= MOD

    for i in range(n, -1, -1):
        if dp[i] != INF:
            print(dp[i])
            break


if __name__ == '__main__':
    main()