Problem p02782 (python)

Fixed submission:
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1000000007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a):
    b = mod
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= mod
    if u < 0:
        u += mod
    return u

fact = [0 for i in range(2000003)]
for i in range(2000003):
    if i <= 1:
        fact[i] = 1
    else:
        fact[i] = (fact[i-1] * i) % mod

# return nCr % p.
def combination(n, r):
    return (((fact[n] * inversion(fact[r])) % mod) * inversion(fact[n-r])) % mod

answer = (
combination(r2 + c2 + 2, r2 + 1) -
combination(r2 + c1 + 1, r2 + 1) -
combination(r1 + c2 + 1, c2 + 1) +
combination(r1 + c1    , c1    ) )

answer %= mod

if answer < 0:
    answer += mod

print(answer)

Buggy submission (verdict: Runtime Error):
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1000000007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a):
    b = mod
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= mod
    if u < 0:
        u += mod
    return u

fact = [0 for i in range(200003)]
for i in range(200003):
    if i <= 1:
        fact[i] = 1
    else:
        fact[i] = (fact[i-1] * i) % mod

# return nCr % p.
def combination(n, r):
    return (((fact[n] * inversion(fact[r])) % mod) * inversion(fact[n-r])) % mod

answer = (
combination(r2 + c2 + 2, r2 + 1) -
combination(r2 + c1 + 1, r2 + 1) -
combination(r1 + c2 + 1, c2 + 1) +
combination(r1 + c1    , c1    ) )

answer %= mod

if answer < 0:
    answer += mod

print(answer)

Buggy submission (verdict: Runtime Error):
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1000000007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a):
    b = mod
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= mod
    if u < 0:
        u += mod
    return u

fact = [0 for i in range(20003)]
for i in range(20003):
    if i <= 1:
        fact[i] = 1
    else:
        fact[i] = (fact[i-1] * i) % mod

# return nCr % p.
def combination(n, r):
    return (((fact[n] * inversion(fact[r])) % mod) * inversion(fact[n-r])) % mod

answer = (
combination(r2 + c2 + 2, r2 + 1) -
combination(r2 + c1 + 1, r2 + 1) -
combination(r1 + c2 + 1, c2 + 1) +
combination(r1 + c1    , c1    ) )

answer %= mod

if answer < 0:
    answer += mod

print(answer)

Buggy submission (verdict: Runtime Error):
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1000000007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a):
    b = mod
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= mod
    if u < 0:
        u += mod
    return u

fact = [0 for i in range(10001)]
for i in range(10001):
    if i <= 1:
        fact[i] = 1
    else:
        fact[i] = fact[i-1] * i % mod

# return nCr % p.
def combination(n, r):
    return ((fact[n] * inversion(fact[r])) % mod) * inversion(fact[n-r]) % mod

answer = (
combination(r2 + c2 + 2, r2 + 1) -
combination(r2 + c1 + 1, r2 + 1) -
combination(r1 + c2 + 1, c2 + 1) +
combination(r1 + c1    , c1    ) )

answer %= mod

if answer < 0:
    answer += mod

print(answer)

Buggy submission (verdict: Runtime Error):
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1000000007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a):
    b = mod
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= mod
    if u < 0:
        u += mod
    return u

fact = [0 for i in range(10001)]
for i in range(10001):
    if i <= 1:
        fact[i] = 1
    else:
        fact[i] = fact[i-1] * i % mod

# return nCr % p.
def combination(n, r):
    return fact[n] * inversion(fact[r]) * inversion(fact[n-r]) % mod

answer = (
combination(r2 + c2 + 2, r2 + 1) -
combination(r2 + c1 + 1, r2 + 1) -
combination(r1 + c2 + 1, c2 + 1) +
combination(r1 + c1    , c1    ) )

answer %= mod

if answer < 0:
    answer += mod

print(answer)

Buggy submission (verdict: Runtime Error):
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1_000_000_007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a, p):
    b = p
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= p
    if u < 0:
        u += p
    return u

# return nCr % p.
def combination(n, r, p):
    if n < r:
        return 0
    r = min(n - r, r)
    nCr = 1
    for d in range(1, r + 1):
        nCr *= n
        nCr %= p
        n -= 1
        nCr *= inversion(d, p)
        nCr %= p
    return nCr

answer = 0
answer += combination(r2 + c2 + 2, r2 + 1, mod)
answer -= combination(r2 + c1 + 1, r2 + 1, mod)
answer -= combination(r1 + c2 + 1, c2 + 1, mod)
answer += combination(r1 + c1    , c1    , mod)

answer %= mod

if answer < 0:
    answer += mod

print(answer)

Buggy submission (verdict: Runtime Error):
r1, c1, r2, c2 = list(map(int, input().split()))

mod = 1_000_000_007

# return an integer k s.t. (a * k) % p = 1 and 0 <= k < p.
def inversion(a, p):
    b = p
    u, v = 1, 0
    while b >= 1:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= p
    if u < 0:
        u += p
    return u

# return nCr % p.
def combination(n, r, p):
    if n < r:
        return 0
    r = min(n - r, r)
    nCr = 1
    for d in range(1, r + 1):
        nCr *= n
        nCr %= p
        n -= 1
        nCr *= inversion(d, p)
        nCr %= p
    return nCr

answer = (
combination(r2 + c2 + 2, r2 + 1, mod) - 
combination(r2 + c1 + 1, r2 + 1, mod) -
combination(r1 + c2 + 1, c2 + 1, mod) +
combination(r1 + c1, c1, mod))

answer %= mod

if answer < 0:
    answer += mod

print(answer)