• + 0 comments

    Python3 solution

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    m = 10 ** 9 + 7
    # parantheses
    p_lim = 201
    # dpp[i][j] is # of paranth. configurations of length i, consisting of j elements
    dpp = [[0 for _ in range(p_lim)] for _ in range(p_lim)]
    dpp[0][0] = 1
    cumsum = [0 for _ in range(p_lim)]
    cumsum[0] = 1
    fact = [1]
    for i in range(1, 250000):
        fact.append((fact[-1] * i) % m)
    
    def egcd(a, b):
        '''
        Extended Euclidian algorithm
        '''
        if a == 0:
            return (b, 0, 1)
        else:
            g, y, x = egcd(b % a, a)
            return (g, x - (b // a) * y, y)
    
    def modinv(a, m):
        '''
        Finds modular inverse modulo m
        '''  
        while a < 0:
            a += m
        g, x, y = egcd(a, m)
        if g != 1:
            raise Exception('Modular inverse does not exist')
        else:
            return x % m
    
    def choose(n, k):
        '''
        Computes binomial efficiently
        '''
        r = (fact[n] * modinv(fact[k], m)) % m
        return (r * modinv(fact[n - k], m)) % m
        
    def C(n): # catalan number
        return (fact[2 * n] * modinv(fact[n + 1], m)) % m
    
    for i in range(1, p_lim):
        dpp[i][1] = cumsum[i - 1] % m
        cumsum[i] = (cumsum[i] + dpp[i][1]) % m
        for j in range(2, p_lim):
            for k in range(1, i - j + 2):
                dpp[i][j] = (dpp[i][j] + dpp[k][1] * dpp[i - k][j - 1]) % m   
            cumsum[i] = (cumsum[i] + dpp[i][j]) % m
    for _ in range(int(input())):
        x, y = map(int,input().split())
        r = 0
        for i in range(y + 1):
            r += dpp[y][i] * choose(2 * x + i, i)
            r %= m
        r = (r * fact[y]) % m
        r = (r * C(x)) % m
        print(r)