• + 0 comments

    Python3 solution

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import sys
    
    def go():
        n, k, p = map(int, sys.stdin.readline().split())
        A = n + 1
        B = k + 1
        res = 1
        f = [1]
        for i in range(1, p):
            f.append(f[-1] * i % p)
        while A or B:
            a, b = A % p, B % p
            A //= p 
            B //= p
            if a < b: return 0
            res *= f[a] * pow(f[b] * f[a-b] % p, p - 2, p)
            res %= p
        return res
    
    for _ in range(int(sys.stdin.readline())):
        print(go())