MATH IT IS ....
Code


# Code
```cpp []
#define ll long long

class Solution {
public:
    ll MOD = 1000000007;
    ll power(ll a, ll b) {
        ll ans = 1;
        a %= MOD;
        while(b) {
            if(b & 1) {
                ans *= a;
                ans %= MOD;
            }
            a *= a;
            a %= MOD;
            b>>=1;
        }
        return ans;
    }

    ll modmul(ll a, ll b) {
        ll c = a;
        c = c * b;
        c = c % MOD;
        return c;
    }

    ll moddiv(ll a, ll b) {
        ll c = a;
        c = c * power(b,MOD-2);
        c = c % MOD;
        return c;
    }

    ll distanceSum(ll m, ll n, ll k) {
        ll N = m*n;
        ll fact[N+1];// calculating the factorials
        fact[0] = 1;
        for(ll i = 1; i <= N; i++) {
            fact[i] = modmul(fact[i-1],i);
        }
        ll ans = moddiv(fact[N-2], modmul(fact[k-2],fact[N-k]));// (m*n-2)C(k-2);

        ll ans2 = 1;
        // formula for all pair wise distances 
        // [ ((n)*(n-1)*(n+1)*m^2)/6 + ((m)*(m-1)*(m+1)*n^2)/6 ] * ((m*n-2)C(k-2)) 
        ans2 = modmul(ans2,m);
        ans2 = modmul(ans2,m-1);
        ans2 = modmul(ans2,m+1);
        ans2 = modmul(ans2,n);
        ans2 = modmul(ans2,n);
        ans2 = moddiv(ans2,6);
        
        ll ans3 = 1;
        ans3 = modmul(ans3,n);
        ans3 = modmul(ans3,n-1);
        ans3 = modmul(ans3,n+1);
        ans3 = modmul(ans3,m);
        ans3 = modmul(ans3,m);
        ans3 = moddiv(ans3,6);
        return modmul(ans,ans2+ans3);
    }
};
```
['Math', 'Combinatorics', 'C++']