Problem p04051 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 10 ** 9 + 7

N = int(readline())
m = map(int,read().split())
AB = zip(m,m)

D = 2000

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U,MOD):
    x = np.arange(U,dtype=np.int64); x[0] = 1
    fact = cumprod(x,MOD)
    x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD)
    fact_inv = cumprod(x,MOD)[::-1]
    return fact,fact_inv

fact,fact_inv = make_fact(10 ** 4,MOD)
fact = fact.tolist()
fact_inv = fact_inv.tolist()

C_to_A = [[] for _ in range(D+D+1)]
remove = 0
for a,b in AB:
    c = a+b
    C_to_A[c].append(D-a)
    remove += fact[c+c] * fact_inv[a+a] * fact_inv[b+b]
remove %= MOD

dp = np.zeros(D+D+1,np.int64)
for A in C_to_A[::-1]:
    dp[1:] += dp[:-1].copy()
    np.add.at(dp,A,1)
    dp %= MOD

x = (dp * dp[::-1] % MOD).sum() % MOD
x -= remove
if x&1:
    x += MOD
x //= 2
x %= MOD

print(x)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 10 ** 9 + 7

N = int(readline())
m = map(int,read().split())
AB = zip(m,m)

D = 2000

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U,MOD):
    x = np.arange(U,dtype=np.int64); x[0] = 1
    fact = cumprod(x,MOD)
    x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD)
    fact_inv = cumprod(x,MOD)[::-1]
    return fact,fact_inv

fact,fact_inv = make_fact(10 ** 4,MOD)
fact = fact.tolist()
fact_inv = fact_inv.tolist()

C_to_A = [[] for _ in range(D+D+1)]
remove = 0
for a,b in AB:
    c = a+b
    C_to_A[c].append(D-a)
    remove += fact[c+c] * fact_inv[a+a] * fact_inv[b+b]
remove %= MOD

dp = np.zeros(D+D+1,np.int64)
for A in C_to_A[::-1]:
    dp[:0:-1] += dp[-1::-1]
    np.add.at(dp,A,1)
    dp %= MOD

x = (dp * dp[::-1] % MOD).sum() % MOD
x -= remove
if x&1:
    x += MOD
x //= 2
x %= MOD

print(x)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

MOD = 10 ** 9 + 7

N = int(readline())
m = map(int,read().split())
AB = zip(m,m)

D = 2000

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U,MOD):
    x = np.arange(U,dtype=np.int64); x[0] = 1
    fact = cumprod(x,MOD)
    x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD)
    fact_inv = cumprod(x,MOD)[::-1]
    return fact,fact_inv

fact,fact_inv = make_fact(D+D+100,MOD)
fact = fact.tolist()
fact_inv = fact_inv.tolist()

C_to_A = [[] for _ in range(D+D+1)]
remove = 0
for a,b in AB:
    c = a+b
    C_to_A[c].append(D-a)
    remove += fact[c+c] * fact_inv[a+a] * fact_inv[b+b]
remove %= MOD

dp = np.zeros(D+D+1,np.int64)
for A in C_to_A[::-1]:
    dp[1:] += dp[:-1]
    np.add.at(dp,A,1)

x = (dp * dp[::-1] % MOD).sum() % MOD
x -= remove
if x&1:
    x += MOD
x //= 2
x %= MOD

print(x)