Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 10**9+7

from collections import defaultdict
from bisect import bisect_right
last = -1
dic = defaultdict(list)
idx = 0
lis = []
for i in range(N):
    c = int(input())
    if c==last:
        continue
    last = c
    dic[c].append(idx)
    lis.append(c)
    idx += 1
memo = {}
memo_s = set()
def dfs(i):
    if i==idx:
        return 1
    if i in memo_s:
        return memo[i]
    v = lis[i]
    ret = 0
    n_idx = bisect_right(dic[v], i)
    if n_idx<len(dic[v]):
        for nx in dic[v][n_idx:]:
            ret += dfs(nx+1)
            ret %= mod
    ret += dfs(i+1)
    ret %= mod
    memo[i] = ret
    memo_s.add(i)
    return ret
print(dfs(0))

Fixed submission:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 10**9+7

last = {}
dp = [0]*(N+1)
dp[0] = 1
for i in range(1, N+1):
    dp[i] = dp[i-1]
    c = int(input())
    if c in last and last[c]+1<i:
        dp[i] += dp[last[c]]
        dp[i] %= mod
    last[c] = i

print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 10**9+7

from collections import defaultdict
from bisect import bisect_right
last = -1
dic = defaultdict(list)
idx = 0
lis = []
for i in range(N):
    c = int(input())
    if c==last:
        continue
    last = c
    dic[c].append(idx)
    lis.append(c)
    idx += 1
memo = {}
def dfs(i):
    if i==idx:
        return 1
    if i in memo.keys():
        return memo[i]
    v = lis[i]
    ret = 0
    n_idx = bisect_right(dic[v], i)
    if n_idx<len(dic[v]):
        for nx in dic[v][n_idx:]:
            ret += dfs(nx+1)
            ret %= mod
    ret += dfs(i+1)
    ret %= mod
    memo[i] = ret
    return ret
print(dfs(0))

Fixed submission:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 10**9+7

last = {}
dp = [0]*(N+1)
dp[0] = 1
for i in range(1, N+1):
    dp[i] = dp[i-1]
    c = int(input())
    if c in last and last[c]+1<i:
        dp[i] += dp[last[c]]
        dp[i] %= mod
    last[c] = i

print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 10**9+7

from collections import defaultdict
last = -1
dic = defaultdict(list)
idx = 0
lis = []
for i in range(N):
    c = int(input())
    if c==last:
        continue
    last = c
    dic[c].append(idx)
    lis.append(c)
    idx += 1
memo = {}
def dfs(i):
    if i==idx:
        return 1
    if i in memo.keys():
        return memo[i]
    v = lis[i]
    ret = 0
    for nx in dic[v]:
        if nx<=i: continue
        ret += dfs(nx+1)
        ret %= mod
    ret += dfs(i+1)
    ret %= mod
    memo[i] = ret
    return ret
print(dfs(0))

Fixed submission:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
mod = 10**9+7

last = {}
dp = [0]*(N+1)
dp[0] = 1
for i in range(1, N+1):
    dp[i] = dp[i-1]
    c = int(input())
    if c in last and last[c]+1<i:
        dp[i] += dp[last[c]]
        dp[i] %= mod
    last[c] = i

print(dp[N])