Problem:
 Score : 1400 points    Problem Statement Snuke is having another barbeque party.  This time, he will make one serving of Skewer Meal .  He has a stock of N  Skewer Meal Packs . The i -th Skewer Meal Pack contains one skewer, A_i pieces of beef and B_i pieces of green pepper.
All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.  To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper.
(Remaining Skewer Meal Packs will not be used.)
Then, all those pieces of food are threaded onto both skewers, one by one, in any order.  (See the image in the Sample section for better understanding.)  In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different.
Since this number can be extremely large, find it modulo 10^9+7 .      Constraints  2≦N≦200,000  1≦A_i≦2000, 1≦B_i≦2000         Input The input is given from Standard Input in the following format:  N  A_1  B_1  A_2  B_2 : A_N  B_N       Output Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo 10^9+7 .        Sample Input 1 3
1 1
1 1
2 1      Sample Output 1 26  The 26 ways of making a Skewer Meal are shown below.
Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer.
Brown and green rectangles represent pieces of beef and green pepper, respectively.

Solution (Python):
import sys
from collections import defaultdict
import numpy as np


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
    return factorials, invs


MOD = 10 ** 9 + 7
n = int(sys.stdin.buffer.readline())
ab = list(map(int, sys.stdin.buffer.read().split()))
aaa = ab[0::2]
bbb = ab[1::2]
max_a = max(aaa)
max_b = max(bbb)
facts, invs = prepare(2 * (max_a + max_b), MOD)
dp_size = max_b * 2 + 1
ans = 0

dd_create_func = lambda: np.zeros(dp_size, dtype=np.int64)
get_ab = defaultdict(dd_create_func)
for a, b in zip(aaa, bbb):
    get_ab[a][b + max_b] += 1
    ans = (ans - facts[2 * (a + b)] * invs[2 * a] * invs[2 * b]) % MOD

dp = np.zeros(dp_size, dtype=np.int64)
for i in range(-max_a, max_a + 1):
    if i < 0 and -i in get_ab:
        dp += get_ab[-i][::-1]
    dp = np.add.accumulate(dp) % MOD
    if i > 0 and i in get_ab:
        ans = (ans + (dp * get_ab[i]).sum()) % MOD

print(ans * invs[2] % MOD)

Solution (Python):
import sys
from collections import defaultdict
import numpy as np


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
    return factorials, invs


MOD = 10 ** 9 + 7
n = int(sys.stdin.buffer.readline())
ab = list(map(int, sys.stdin.buffer.read().split()))
aaa = ab[0::2]
bbb = ab[1::2]
max_a = max(aaa)
max_b = max(bbb)
facts, invs = prepare(2 * (max_a + max_b), MOD)
dp_size = max_b * 2 + 1
ans = 0

dd_create_func = lambda: np.zeros(dp_size, dtype=np.int64)
put_ab = defaultdict(dd_create_func)
get_ab = defaultdict(dd_create_func)
for a, b in zip(aaa, bbb):
    put_ab[-a][-b + max_b] += 1
    get_ab[a][b + max_b] += 1
    ans = (ans - facts[2 * (a + b)] * invs[2 * a] * invs[2 * b]) % MOD

dp = np.zeros(dp_size, dtype=np.int64)
for i in range(-max_a, max_a + 1):
    if i < 0 and i in put_ab:
        dp += put_ab[i]
    dp = np.add.accumulate(dp) % MOD
    if i > 0 and i in get_ab:
        ans = (ans + (dp * get_ab[i]).sum()) % MOD

print(ans * invs[2] % MOD)

Solution (Python):
import sys
from collections import defaultdict

import numpy as np


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
    return factorials, invs


n = int(sys.stdin.buffer.readline())
mp = map(int, sys.stdin.buffer.read().split())
ab = list(zip(mp, mp))
atob = defaultdict(list)
max_a, max_b = 0, 0
for a, b in ab:
    atob[a].append(b)
    max_a = max(max_a, a)
    max_b = max(max_b, b)

MOD = 10 ** 9 + 7

dp = np.zeros(max_b * 2 + 1, dtype=np.int64)
ans = 0
for i in range(-max_a, max_a + 1):
    if i < 0 and -i in atob:
        for j in atob[-i]:
            dp[-j + max_b] += 1
    dp = np.add.accumulate(dp) % MOD
    if i > 0 and i in atob:
        for j in atob[i]:
            ans = (ans + dp[j + max_b]) % MOD

facts, invs = prepare(2 * (max_a + max_b), MOD)
ans = int(ans)
for a, b in ab:
    ans = (ans - facts[2 * (a + b)] * invs[2 * a] * invs[2 * b]) % MOD

print(ans * invs[2] % MOD)

Solution (Python):
mod = 10**9+7
mod2 = 998244353
rng = 100100
fctr = [1]
finv = [1]
for i in range(1,rng):
  fctr.append(fctr[-1]*i%mod)
for i in range(1,rng):
  finv.append(pow(fctr[i],mod-2,mod))
def cmb(n,k):
  if n<0 or k<0:
    return 0
  else:
    return fctr[n]*finv[n-k]*finv[k]%mod
n = int(input())
ab = [list(map(int,input().split())) for i in range(n)]
s = 0
for a,b in ab:
  s = max(s,a,b)
dp = [[0 for i in range(2*s+2)] for j in range(2*s+2)]
for a,b in ab:
  dp[s+1-a][s+1-b] += 1
for i in range(1,2*s+2):
  for j in range(1,2*s+2):
    dp[i][j] = (dp[i][j]+dp[i-1][j]+dp[i][j-1])%mod
x = 0
for a,b in ab:
  x = (x+dp[s+1+a][s+1+b])%mod
t = 0
for a,b in ab:
  t = (t+cmb(2*a+2*b,2*a))%mod
ans = (x-t)*pow(2,mod-2,mod)%mod
print(ans)

Solution (Python):
import numpy as np

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
mod = int(1e+9) + 7
def extgcd(a, b):
  if b == 0:
    return 1, 0
  else:
    x, y, u, v, k, l = 1, 0, 0, 1, a, b
    while l != 0:
      x, y, u, v = u, v, x - u * (k // l), y - v * (k // l)
      k, l = l, k % l
    return x, y
def inved(x):
  a, b = extgcd(x, mod)
  return a % mod

fact = [1 for i in range(8001)]
finv = [0 for i in range(8001)]
for i in range(8000):
  fact[i+1] = (fact[i] * (i + 1)) % mod
finv[8000] = inved(fact[8000])
for i in range(8000, 0, -1):
  finv[i-1] = (finv[i] * i) % mod
Y = 0
for i in range(N):
  Y += (fact[2*(A[i][0]+A[i][1])]*finv[2*A[i][0]]*finv[2*A[i][1]])%mod
  Y %= mod
B = [[] for i in range(4001)]
for i in A:
  B[i[0]+i[1]].append(2000-i[0])
f = np.zeros(2000+2000+1,np.int64)
for b in B[::-1]:
  f[1:] += f[:-1].copy()
  np.add.at(f,b,1)
  f %= mod
X = (f * f[::-1] % mod).sum() % mod

print(((X-Y) * inved(2))%mod)

Solution (Python):
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

U = 10 ** 4
fact = [1] * U
for n in range(1,U):
    fact[n] = fact[n-1] * n % MOD
fact_inv = [1] * U
fact_inv[-1] = pow(fact[-1],MOD-2,MOD)
for n in range(U-1,0,-1):
    fact_inv[n-1] = fact_inv[n] * n % MOD

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

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

answer = (X - Y) * fact_inv[2] % MOD
print(answer)

Solution (Python):
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

U = 10 ** 4
fact = [1] * U
for n in range(1,U):
    fact[n] = fact[n-1] * n % MOD
fact_inv = [1] * U
fact_inv[-1] = pow(fact[-1],MOD-2,MOD)
for n in range(U-1,0,-1):
    fact_inv[n-1] = fact_inv[n] * n % MOD

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]

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 % MOD
x *= fact_inv[2]
x %= MOD

print(x)

Solution (Python):
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[-2::-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)

Solution (Python):
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)

Solution (Python):
from collections import defaultdict
def solve():
	Base = 2000
	MAX = Base*2
	p = 10**9+7
	F = [1]; Finv = [0]*(MAX*2+1)
	for i in xrange(MAX*2): F.append(F[i]*(i+1)%p)
	Finv[MAX] = pow(F[MAX],p-2,p)
	for i in xrange(MAX,0,-1): Finv[i-1] = Finv[i]*i%p
	N = int(raw_input())
	dup = A_MAX = B_MAX = 0
	dp = [[0]*(MAX+2) for i in xrange(MAX+2)]
	goals = defaultdict(int)
	for i in xrange(N):
		A,B = map(int,raw_input().split())
		dup = (dup + F[A*2+B*2]*Finv[A*2]*Finv[B*2])%p
		if A > A_MAX: A_MAX = A
		if B > B_MAX: B_MAX = B
		goals[(Base+A,Base+B)] += 1
		dp[Base-B][Base-A] += 1
	for j in xrange(Base-B_MAX,Base+B_MAX+1):
		for i in xrange(Base-A_MAX,Base+A_MAX+1):
			dp[j+1][i] = (dp[j+1][i] + dp[j][i]) % p
			dp[j][i+1] = (dp[j][i+1] + dp[j][i]) % p
	ans = 0
	for (i,j),v in goals.iteritems():
		ans = (ans + dp[j][i]*v) % p
	print ((ans - dup)*pow(2,p-2,p)+p) % p
solve()