Problem p03287 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
#def input():
#    return sys.stdin.readline()[:-1]

'''
n=int(input())
for i in range(n):
    a[i]=int(input())
    a[i],b[i]=map(int,input().split())
a=[int(x) for x in input().split()]
n,m=map(int,input.split())
from operator import itemgetter
a = [(1, "c", 1), (1, "b", 3), (2, "a", 0), (1, "a", 2)]
print(sorted(a))  # 0 番目の要素でソート、先頭の要素が同じなら 1 番目以降の要素も見る
print(sorted(a, key=itemgetter(0)))  # 0 番目の要素だけでソート
print(sorted(a, key=itemgetter(0, 2)))  # 0 番目と 2 番目の要素でソート
print(sorted(a, key=lambda x: x[0] * x[2]))  # 0 番目の要素 * 2 番目の要素でソート
print(sorted(a, reverse=True))  # 降順にソート
a.sort()  # 破壊的にソート、sorted() よりも高速
try: # エラーキャッチ list index out of range
    for i in range():
        k=b[i]
except IndexError as e:
    print(i)
'''

test_data1 = '''\
3 2
4 1 5
'''

test_data2 = '''\
13 17
29 7 5 7 9 51 7 13 8 55 42 9 81
'''

test_data3 = '''\
10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
'''
td_num=3

def GetTestData(index):
    if index==1:
        return test_data1
    if index==2:
        return test_data2
    if index==3:
        return test_data3
    
if False:
    with open("../test.txt", mode='w') as f:
        f.write(GetTestData(td_num))
    with open("../test.txt") as f:
        # Start Input code ---------------------------------------
        n,m=map(int,f.readline().split())
        a=list(map(int,f.readline().split()))
        # End Input code ---------------------------------------
else:
    # Start Input code ---------------------------------------
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    # End Input code ---------------------------------------

pre=0
acc=[0]*n
cnt=dict()
for i in range(n):
    pre+=a[i]
    pre=pre%m
    acc[i]=pre
    if pre in cnt:
        cnt[pre]+=1
    else:
        cnt[pre]=1
for i in range(n):
    acc
def comb(nn,rr):
    rr=min(rr,nn-rr)
    rt=1
    for i in range(rr):
        #rt=rt*(nn-i)/(rr-i)
        rt*=(nn-i)
    for i in range(rr):
        rt//=(rr-i)
    return int(rt)
ans=0
for i in cnt:
    cc=cnt[i]
    if cc>1: ans+=comb(cc,2)
if 0 in cnt: ans+=cnt[0]
#print(ans,'n,m=',n,m,' a=',a,'acc=',acc,'cnt=',cnt)
print(ans)
#ans=count(0)+2^(count(n)-1)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
#def input():
#    return sys.stdin.readline()[:-1]

'''
n=int(input())
for i in range(n):
    a[i]=int(input())
    a[i],b[i]=map(int,input().split())
a=[int(x) for x in input().split()]
n,m=map(int,input.split())
from operator import itemgetter
a = [(1, "c", 1), (1, "b", 3), (2, "a", 0), (1, "a", 2)]
print(sorted(a))  # 0 番目の要素でソート、先頭の要素が同じなら 1 番目以降の要素も見る
print(sorted(a, key=itemgetter(0)))  # 0 番目の要素だけでソート
print(sorted(a, key=itemgetter(0, 2)))  # 0 番目と 2 番目の要素でソート
print(sorted(a, key=lambda x: x[0] * x[2]))  # 0 番目の要素 * 2 番目の要素でソート
print(sorted(a, reverse=True))  # 降順にソート
a.sort()  # 破壊的にソート、sorted() よりも高速
try: # エラーキャッチ list index out of range
    for i in range():
        k=b[i]
except IndexError as e:
    print(i)
'''

test_data1 = '''\
3 2
4 1 5
'''

test_data2 = '''\
13 17
29 7 5 7 9 51 7 13 8 55 42 9 81
'''

test_data3 = '''\
10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
'''
td_num=3

def GetTestData(index):
    if index==1:
        return test_data1
    if index==2:
        return test_data2
    if index==3:
        return test_data3
    
if True:
    with open("../test.txt", mode='w') as f:
        f.write(GetTestData(td_num))
    with open("../test.txt") as f:
        # Start Input code ---------------------------------------
        n,m=map(int,f.readline().split())
        a=list(map(int,f.readline().split()))
        # End Input code ---------------------------------------
else:
    # Start Input code ---------------------------------------
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    # End Input code ---------------------------------------

pre=0
acc=[0]*n
cnt=dict()
for i in range(n):
    pre+=a[i]
    pre=pre%m
    acc[i]=pre
    if pre in cnt:
        cnt[pre]+=1
    else:
        cnt[pre]=1
for i in range(n):
    acc
def comb(nn,rr):
    rr=min(rr,nn-rr)
    rt=1
    for i in range(rr):
        #rt=rt*(nn-i)/(rr-i)
        rt*=(nn-i)
    for i in range(rr):
        rt//=(rr-i)
    return int(rt)
ans=0
for i in cnt:
    cc=cnt[i]
    if cc>1: ans+=comb(cc,2)
if 0 in cnt: ans+=cnt[0]
#print(ans,'n,m=',n,m,' a=',a,'acc=',acc,'cnt=',cnt)
print(ans)
#ans=count(0)+2^(count(n)-1)