Easy Solution C# - Beat 100% - Mathematic Combinatorics
Code
![Screenshot 2025-01-19 001101.png](https://assets.leetcode.com/users/images/83f3c34c-7487-468d-aa7f-8aba35c1b2cd_1737220272.5544698.png)
# Code
```csharp []
using System;
using System.Collections.Generic;
public class Solution {
    public class Combinatorics {
        private long n, MOD;
        private List<long> fact, inv, factInv;

        public Combinatorics(long n, long MOD) {
            this.n = n;
            this.MOD = MOD;
            fact = new List<long>(new long[n + 1]);
            inv = new List<long>(new long[n + 1]);
            factInv = new List<long>(new long[n + 1]);

            inv[1] = 1;
            for (long i = 2; i <= n; ++i) {
                inv[(int)i] = MOD - (MOD / i) * inv[(int)(MOD % i)] % MOD;
            }

            fact[0] = factInv[0] = 1;
            for (long i = 1; i <= n; ++i) {
                fact[(int)i] = fact[(int)(i - 1)] * i % MOD;
                factInv[(int)i] = factInv[(int)(i - 1)] * inv[(int)i] % MOD;
            }
        }

        public long GetNChooseK(long n, long k) {
            if (n < 0 || k < 0 || n < k)
                return 0;
            return fact[(int)n] * factInv[(int)k] % MOD * factInv[(int)(n - k)] % MOD;
        }

        public long GetFact(long i) {
            return fact[(int)i];
        }

        public long GetFactInv(long i) {
            return factInv[(int)i];
        }

        public long GetInv(long i) {
            return inv[(int)i];
        }
    }

    public int DistanceSum(int m, int n, int k) {
        long ans = 0;
        long MOD = 1_000_000_007;
        var comb = new Combinatorics(100001, MOD);
        long sum = 0;
        for (long i = 1; i <= m; ++i) {
            sum += ((i * i - i * (i + 1) / 2) % MOD) * (n * n % MOD) % MOD;
            sum %= MOD;
        }
        for (long i = 1; i <= n; ++i) {
            sum += ((i * i - i * (i + 1) / 2) % MOD) * (m * m % MOD) % MOD;
            sum %= MOD;
        }
        return (int)(sum * comb.GetNChooseK(m * n - 2, k - 2) % MOD);
    }
}

```
['C#']