• + 1 comment

    Solution in Python 3

    import sys
    
    P = 10**9 + 7
    
    fact = [1]
    for i in range(1, 200001):
    	fact.append(fact[-1] * i % P)
    
    inv = [0, 1]
    for i in range(2, 200001):
    	inv.append(P - P // i * inv[P % i] % P)
    inv_fact = [1]
    for i in range(1, 100001):
    	inv_fact.append(inv_fact[-1] * inv[i] % P)
    
    def ceil_div(a, b):
    	return -(-a // b)
    
    def part(n, k):
    	# print(n, k)
    	if k == 0:
    		return 1 if n == 0 else 0
    	return C(n + k - 1, n)
    
    def C(n, k):
    	return fact[n] * inv_fact[k] % P * inv_fact[n - k] % P if (n >= 0) and (k >= 0) and (k <= n) else 0
    
    def g(sum, n, b):
    	# print(sum, n, b)
    	ret = 0
    	for i in range(sum // b + 1):
    		sign = 1 if i & 1 == 0 else -1
    		ret += sign * part(sum - i * b, n) * C(n, i) % P
    		ret %= P
    	# print (ret)
    	return ret
    
    def f(n, k):
    	ret = 0
    	for max in range(ceil_div(n + k - 1, k), n + 1):
    		ret += g(n - max, k - 1, max)
    		ret %= P
    	return ret * k % P
    
    # print(g(6, 2, 4))
    
    t = int(input())
    
    for _ in range(t):
    	n, low, high = map(int, input().split())
    	print(f(n, high - low + 1))