Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)

def dfs(G, p, pre, t, edge, x):
    for nxt in G[p]:
        if nxt == pre:
            continue
        a, b = p, nxt
        if b < a:
            a, b = b, a
        if nxt == t:
            edge[(a, b)] += x
            return True
        if dfs(G, nxt, p, t, edge, x):
            edge[(a, b)] += x
            return True
    return False

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    edge = {}
    for _ in range(n-1):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        G[a].append(b)
        G[b].append(a)
        if b < a:
            a, b = b, a
        edge[(a, b)] = 0
    m = int(input())
    for i in range(m):
        u, v = map(int, input().split())
        u, v = u-1, v-1
        x = 1<<i
        dfs(G, u, -1, v, edge, x)
    ans = 2**(n-1)
    for i in range(1, 2**m):
        bit_cnt = 0
        for j in range(m):
            if (i & (1<<j)) != 0:
                bit_cnt += 1
        cnt = 0
        for v in edge.values():
            if (v & i) != 0:
                cnt += 1
        x = 2**(n-1 - cnt)
        if bit_cnt % 2 == 0:
            ans += x
        else:
            ans -= x
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

def dfs(G, p, pre, t, edge, x):
    for nxt in G[p]:
        if nxt == pre:
            continue
        a, b = p, nxt
        if b < a:
            a, b = b, a
        if nxt == t:
            edge[(a, b)] += x
            return True
        if dfs(G, nxt, p, t, edge, x):
            edge[(a, b)] += x
            return True
    return False

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    edge = {}
    for _ in range(n-1):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        G[a].append(b)
        G[b].append(a)
        if b < a:
            a, b = b, a
        edge[(a, b)] = 0
    m = int(input())
    for i in range(m):
        u, v = map(int, input().split())
        u, v = u-1, v-1
        x = 1<<i
        dfs(G, u, -1, v, edge, x)
    ans = 2**(n-1)
    for i in range(1, 2**m):
        bit_cnt = 0
        for j in range(m):
            if (i & (1<<j)) != 0:
                bit_cnt += 1
        cnt = 0
        for v in edge.values():
            if (v & i) != 0:
                cnt += 1
        x = 2**(n-1 - cnt)
        if bit_cnt % 2 == 0:
            ans += x
        else:
            ans -= x
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    tmp = list(map(int, input().split()))
    h = [0]
    for v in tmp:
        h.append(v)
    dp = [[float("inf")]*(n-k+1) for i in range(n+1)]
    dp[0][0] = 0
    for i in range(1, n+1):
        for j in range(1, n-k+1):
            tmp = float("inf")
            for l in range(i):
                p = max(0, h[i] - h[l])
                if tmp > dp[l][j-1] + p:
                    tmp = dp[l][j-1] + p
            dp[i][j] = tmp
    ans = float("inf")
    for i in range(n+1):
        if ans > dp[i][n-k]:
            ans = dp[i][n-k]
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    n, k = map(int, input().split())
    tmp = list(map(int, input().split()))
    h = [0]
    for v in tmp:
        h.append(v)
    dp = [[float("inf")]*(n-k+1) for i in range(n+1)]
    dp[0][0] = 0
    for i in range(1, n+1):
        for j in range(1, n-k+1):
            tmp = float("inf")
            for l in range(i):
                p = max(0, h[i] - h[l])
                if tmp > dp[l][j-1] + p:
                    tmp = dp[l][j-1] + p
            dp[i][j] = tmp
    ans = float("inf")
    for i in range(n+1):
        if ans > dp[i][n-k]:
            ans = dp[i][n-k]
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

def main():
    n = int(input())
    a = []
    for _ in range(n):
        target = int(input())
        target = 10**9 + 1 - target 
        index = bisect.bisect_left(a, target)
        if index < len(a) and a[index] > target:
            a[index] = target
        else:
            a.append(target)
        a.sort()
    print(len(a))

if __name__ == "__main__":
    main()


Fixed submission:
import bisect

def main():
    n = int(input())
    buf = []
    a = [None] * n
    for i in range(n):
        a[i] = int(input())
    for i in reversed(a):
        index = bisect.bisect_right(buf, i)
        if index < len(buf) and buf[index] > i:
            buf[index] = i
        else:
            buf.append(i)
    print(len(buf))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 7)

def solve(g, p, index, y, z, k):
    p[index] = k - y - z
    x = 1
    for i in g[index]:
        if p[i] == -1:
            solve(g, p, i, x, y, k)
            y += 1

def main():
    n, k = map(int, input().split())
    graph =[[] for _ in range(n)]
    p = [-1]*n
    for _ in range(n-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    s = None
    for i in range(n):
        if len(graph[i]) == 1:
            s = i
            break
    solve(graph, p, s, 0, 0, k)
    ans = 1
    for i in p:
        ans *= i
        ans %= 10**9 + 7
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

def solve(g, p, index, y, z, k):
    p[index] = k - y - z
    x = 1
    for i in g[index]:
        if p[i] == -1:
            solve(g, p, i, x, y, k)
            y += 1

def main():
    n, k = map(int, input().split())
    graph =[[] for _ in range(n)]
    p = [-1]*n
    for _ in range(n-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    s = None
    for i in range(n):
        if len(graph[i]) == 1:
            s = i
            break
    if s is None:
        print(k)
        sys.exit()
    solve(graph, p, s, 0, 0, k)
    ans = 1
    for i in p:
        ans *= i
        ans %= 10**9 + 7
    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def solve(g, p, index, y, z, k):
    p[index] = k - y - z
    x = 1
    for i in g[index]:
        if p[i] == -1:
            solve(g, p, i, x, y, k)
            y += 1

def main():
    n, k = map(int, input().split())
    graph =[[] for _ in range(n)]
    p = [-1]*n
    for _ in range(n-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    s = None
    for i in range(n):
        if len(graph[i]) == 1:
            s = i
            break
    solve(graph, p, s, 0, 0, k)
    ans = 1
    for i in p:
        ans *= i
        ans %= 10**9 + 7
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

def solve(g, p, index, y, z, k):
    p[index] = k - y - z
    x = 1
    for i in g[index]:
        if p[i] == -1:
            solve(g, p, i, x, y, k)
            y += 1

def main():
    n, k = map(int, input().split())
    graph =[[] for _ in range(n)]
    p = [-1]*n
    for _ in range(n-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    s = None
    for i in range(n):
        if len(graph[i]) == 1:
            s = i
            break
    if s is None:
        print(k)
        sys.exit()
    solve(graph, p, s, 0, 0, k)
    ans = 1
    for i in p:
        ans *= i
        ans %= 10**9 + 7
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
from math import sqrt
MOD = 10**9 + 7

def main():
    lim, n = map(int, input().split())
    num, tmp = [], []
    for i in range(1, int(sqrt(lim))+1):
        num.append(i)
        if i**2 != lim:
            tmp.append(lim//i)
    while len(tmp) > 0:
        num.append(tmp.pop(-1))
    m = len(num)
    dp = [[0]*m for _ in range(n)]
    dp[0][0] = 1
    for j in range(1, m):
        dp[0][j] = num[j] - num[j-1]
    for i in range(1, n):
        for j in range(1, m):
            dp[i-1][j] += dp[i-1][j-1]
        for j in range(m):
            idx = bisect_left(num, lim//num[j])
            p = num[j]
            if j > 0:
                p -= num[j-1]
            dp[i][j] = dp[i-1][idx] * p % MOD
    print(sum(dp[-1])%MOD)

if __name__ == "__main__":
    main()

Fixed submission:
from bisect import bisect_left
from math import sqrt
MOD = 10**9 + 7

def main():
    lim, n = map(int, input().split())
    num, tmp = [], []
    for i in range(1, int(sqrt(lim))+1):
        num.append(i)
        if i**2 != lim:
            tmp.append(lim//i)
    while len(tmp) > 0:
        num.append(tmp.pop(-1))
    m = len(num)
    dp = [[0]*m for _ in range(n)]
    dp[0][0] = 1
    for j in range(1, m):
        dp[0][j] = num[j] - num[j-1]
    for i in range(1, n):
        for j in range(1, m):
            dp[i-1][j] += dp[i-1][j-1]
        for j in range(m):
            idx = bisect_left(num, lim//num[j])
            p = num[j]
            if j > 0:
                p -= num[j-1]
            dp[i][j] = dp[i-1][idx] * p % MOD
    print(sum(dp[-1])%MOD)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
MOD = 10**9 + 7
MOD_t_MAX = 10*5+3
fac  = [None] * MOD_t_MAX
finv = [None] * MOD_t_MAX
inv  = [None] * MOD_t_MAX
def MOD_COM_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MOD_t_MAX):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD
def MOD_COM(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
def MOD_pow(a, n):
    res = 1
    while n > 0:
        if (n & 1):
            res = res * a % MOD
        a = a * a % MOD
        n = n // 2
    return res

def main():
    L = input().rstrip()
    MOD_COM_init()
    cnt = [0]*2
    for i in range(len(L)):
        if L[i] == '0':
            cnt[0] += 1
        else:
            cnt[1] += 1
    cnt[1] -= 1
    ans = 0
    for i in range(cnt[1]+1):
        buf1 = MOD_COM(cnt[1], i)
        buf2 = MOD_pow(2, cnt[1]-i)
        ans += buf1 * buf2 % MOD
        ans = ans % MOD
    ans = ans * 2
    for i in range(len(L)):
        buf1 = MOD_COM(len(L)-1, i)
        buf2 = MOD_pow(2, len(L)-1-i)
        ans += buf1 * buf2 % MOD
        ans = ans % MOD
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    L = input().rstrip()
    MOD = 10**9 + 7
    dp = [[0 for equal in range(2)] for digit in range(len(L) + 1)]
    dp[0][0] = 1
    for digit in range(len(L)):
        x = int(L[digit])
        if x == 1:
            dp[digit+1][1] += dp[digit][0] + dp[digit][1]
            dp[digit+1][0] += dp[digit][0] * 2
            dp[digit+1][1] += dp[digit][1] * 2
        else:
            dp[digit+1][0] += dp[digit][0]
            dp[digit+1][1] += dp[digit][1]
            dp[digit+1][1] += dp[digit][1] * 2
        dp[digit+1][0] %= MOD
        dp[digit+1][1] %= MOD
    print((dp[len(L)][0] + dp[len(L)][1])%MOD)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
#    v_cum = [x for x in v]
#    for i in range(1, n):
#        v_cum[i] += v_cum[i-1]
    ans = 0
    for L_i in range(min(k+1, n)):#左から取る回数
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):#右から取る回数
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
#            print(L_i, R_i, tmp)
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
#    v_cum = [x for x in v]
#    for i in range(1, n):
#        v_cum[i] += v_cum[i-1]
    ans = 0
    for L_i in range(min(k+1, n)):#左から取る回数
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):#右から取る回数
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
#            print(L_i, R_i, tmp)
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for L_i in range(min(k+1, n)):#左から取る回数
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):#右から取る回数
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from math import sqrt
def main():
    n = int(input())
    ans = 0
    for i in range(1, int(sqrt(n))+1):
        y = n - i
        if y % i == 0 and n//(y//i) == i:
            ans += y // i
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
from math import sqrt
def main():
    n = int(input())
    ans = 0
    for i in range(1, int(sqrt(n))+1):
        y = n - i
        if y % i == 0 and y != 0 and n//(y//i) == i:
            ans += y // i
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import queue
def main():
    h, w = map(int, input().split())
    s = [input().rstrip() for _ in range(h)]
    g = [[0 for j in range(w)] for i in range(h)]
    num = 1
    for i in range(h):
        for j in range(w):
            if g[i][j] != 0:
                continue
            q = queue.Queue()
            q.put([i, j])
            g[i][j] = num
            while not q.empty():
                y, x = q.get()
                if 0 < y and g[y-1][x] == 0 and s[y][x] != s[y-1][x]:
                    g[y-1][x] = num
                    q.put([y-1, x])
                if y < h-1 and g[y+1][x] == 0 and s[y][x] != s[y+1][x]:
                    g[y+1][x] = num
                    q.put([y+1, x])
                if 0 < x and g[y][x-1] == 0 and s[y][x] != s[y][x-1]:
                    g[y][x-1] = num
                    q.put([y, x-1])
                if x < w-1 and g[y][x+1] == 0 and s[y][x] != s[y][x+1]:
                    g[y][x+1] = num
                    q.put([y, x+1])
            num += 1
    blk, wht = [0]*(num+1), [0]*(num+1)
    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                blk[g[i][j]] += 1
            else:
                wht[g[i][j]] += 1
    ans = 0
    for i in range(num+1):
        ans += blk[i] * wht[i]
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import queue
def main():
    h, w = map(int, input().split())
    s = [input().rstrip() for _ in range(h)]
    g = [[0 for j in range(w)] for i in range(h)]
    num = 1
    q = queue.Queue()
    for i in range(h):
        for j in range(w):
            if g[i][j] != 0:
                continue
            q.put([i, j])
            g[i][j] = num
            while not q.empty():
                y, x = q.get()
                if 0 < y and g[y-1][x] == 0 and s[y][x] != s[y-1][x]:
                    g[y-1][x] = num
                    q.put([y-1, x])
                if y < h-1 and g[y+1][x] == 0 and s[y][x] != s[y+1][x]:
                    g[y+1][x] = num
                    q.put([y+1, x])
                if 0 < x and g[y][x-1] == 0 and s[y][x] != s[y][x-1]:
                    g[y][x-1] = num
                    q.put([y, x-1])
                if x < w-1 and g[y][x+1] == 0 and s[y][x] != s[y][x+1]:
                    g[y][x+1] = num
                    q.put([y, x+1])
            num += 1
    blk, wht = [0]*(num+1), [0]*(num+1)
    for i in range(h):
        for j in range(w):
            if s[i][j] == "#":
                blk[g[i][j]] += 1
            else:
                wht[g[i][j]] += 1
    ans = 0
    for i in range(num+1):
        ans += blk[i] * wht[i]
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**9+1]*n
    dp[0] = 0
    for i in range(n-1):
        for j in range(i+1, min(n, i+k+1)):
            if dp[i] + abs(h[i] - h[j]) < dp[j]:
                dp[j] = dp[i] + abs(h[i] - h[j])
    print(dp[n-1])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**9+1]*n
    dp[0] = 0
    for i in range(1, n):
        buf = [dp[j] + abs(h[j]-h[i]) for j in range(max(i-k,0), i)]
        dp[i] = min(buf)
    print(dp[n-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**9+1]*n
    dp[0] = 0
    for i in range(n-1):
        for j in range(1, k+1):
            if n <= i+j:
                break
            if dp[i] + abs(h[i] - h[i+j]) < dp[i+j]:
                dp[i+j] = dp[i] + abs(h[i] - h[i+j])
    print(dp[n-1])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**9+1]*n
    dp[0] = 0
    for i in range(1, n):
        buf = [dp[j] + abs(h[j]-h[i]) for j in range(max(i-k,0), i)]
        dp[i] = min(buf)
    print(dp[n-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**10]*n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k+1):
            if i + j < n and dp[i] + abs(h[i] - h[i+j]) < dp[i+j]:
                dp[i+j] = dp[i] + abs(h[i] - h[i+j])
    print(dp[n-1])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    dp = [10**9+1]*n
    dp[0] = 0
    for i in range(1, n):
        buf = [dp[j] + abs(h[j]-h[i]) for j in range(max(i-k,0), i)]
        dp[i] = min(buf)
    print(dp[n-1])

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    dic = {}
    for v in a:
        if v in dic:
            dic[v] += 1
        else:
            dic[v] = 1
    key = [v for v in dic.keys()]
    key.sort(reverse=True)
    ans = 0
    for v in key:
        num = dic[v]
        if num > 0:
            for i in reversed(range(1, 35)):
                target = 2**i
                if 2*v < target or target < v:
                    continue
                u = target - v
                if v == u:
                    z = dic[v]//2
                    ans += z
                    dic[v] -= 2*z
                elif u in dic:
                    x, y = dic[v], dic[u]
                    z = 0
                    if x < y:
                        z = x
                    else:
                        z = y
                    dic[v] -= z
                    dic[u] -= z
                    ans += z
                if dic[v] == 0:
                    break
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    dic = {}
    for v in a:
        if v in dic:
            dic[v] += 1
        else:
            dic[v] = 1
    key = [v for v in dic.keys()]
    key.sort(reverse=True)
    ans = 0
    for v in key:
        num = dic[v]
        if num > 0:
            for i in reversed(range(1, 32)):
                target = 2**i
                if 2*v < target:
                    continue
                if target < v:
                    break
                u = target - v
                if v == u:
                    z = dic[v]//2
                    ans += z
                    dic[v] -= 2*z
                elif u in dic:
                    x, y = dic[v], dic[u]
                    z = 0
                    if x < y:
                        z = x
                    else:
                        z = y
                    dic[v] -= z
                    dic[u] -= z
                    ans += z
                if dic[v] == 0:
                    break
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<limits.h>

#define rep(i,begin,end) for(int i=begin; i<end; i++)
#define revrep(i,begin,end) for(int i=begin; i>end; i--)
#define lld long long int

int cmpAsc(const void* a, const void* b){
    int x = *(int*)a, y = *(int*)b;
    if(x > y){
        return 1;
    }
    if(x < y){
        return -1;
    }
    return 0;
}

int main(){
    int n;
    lld m;
    scanf("%d %lld", &n, &m);
    lld a[n+1];
    a[0] = 0;
    rep(i, 1, n+1){
        scanf("%lld", &a[i]);
    }
    rep(i, 1, n+1){
        a[i] += a[i-1];
        a[i] %= m;
    }
    qsort(a, n+1, sizeof(lld), cmpAsc);
    int cnt = 1, target = -1;
    lld ans = 0;
    rep(i, 0, n+1){
        if(a[i] == target){
            ans += (lld)cnt;
            cnt++;
        }else{
            target = a[i];
            cnt = 1;
        }
    }
    printf("%lld", ans);

    return 0;
}

Fixed submission:
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    a[0] = a[0]%m
    for i in range(1, n):
        a[i] += a[i-1]
        a[i] %= m
    a.sort()
    target = 0
    cnt = 1
    for i in a:
        if i == target:
            ans += cnt
            cnt += 1
        else:
            target = i
            cnt = 1
    print(ans)

if __name__ == "__main__":
    main()