Problem: We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.  If we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the _cost_ of this arrangement is computed as:  \sum_{i=1}^{K-1} \sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)  Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.  We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.

Submission attempts (in order):
from scipy.misc import comb


def solve(string):
    n, m, k = map(int, string.split())
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % (10**9 + 7)
    return str(base * comb(n * m - 2, k - 2, exact=True) % (10**9 + 7))


if __name__ == '__main__':
    print(solve(input()))


from scipy.misc import comb


def solve(string):
    n, m, k = map(int, string.split())
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % (10**9 + 7)
    return str(base * comb(n * m - 2, min(k - 2, n * m - k), exact=True) % (10**9 + 7))


if __name__ == '__main__':
    print(solve(input()))


from scipy.misc import comb


def solve(string):
    n, m, k = map(int, string.split())
    return str(n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 *
               comb(n * m - 2, k - 2, exact=True) % (10**9 + 7))


if __name__ == '__main__':
    print(solve(input()))


from scipy.misc import comb


def solve(string):
    n, m, k = map(int, string.split())
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % (10**9 + 7)
    return str(base * comb(n * m - 2, k - 2, exact=True) % (10**9 + 7))


if __name__ == '__main__':
    print(solve(input()))


from scipy.misc import comb


def solve(string):
    n, m, k = map(int, string.split())
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % (10**9 + 7)
    return str(base * (comb(n * m - 2, k - 2, exact=True) % (10**9 + 7)) % (10**9 + 7))


if __name__ == '__main__':
    print(solve(input()))


from scipy.misc import comb


def solve(string):
    n, m, k = map(int, string.split())
    p = 10**9 + 7
    base = n * m * ((n + 1) * (m + 1) * (n + m - 2) - n**2 - m**2 + 2) // 6 % p
    return str(base * (comb(n * m - 2, min(k - 2, n * m - k), exact=True) % p) % p)


if __name__ == '__main__':
    print(solve(input()))


def mulmod(x, y, mod=10**9 + 7):
    return x * y % mod


def comb(n, k, mod=10**9 + 7):
    from functools import reduce
    k = min(k, n - k)
    x = reduce(mulmod, range(n - k + 1, n + 1), 1)
    y = reduce(mulmod, range(1, k + 1), 1)
    return x * pow(y, mod - 2, mod) % mod


def solve(string):
    n, m, k = map(int, string.split())
    mod = 10**9 + 7
    base = n * m * (n + m) * (n * m - 1) // 6 % mod
    return str(base * comb(n * m - 2, k - 2) % mod)


if __name__ == '__main__':
    print(solve(input()))


Verdicts: Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Accepted