structCombination { vector<longlong> fac, inv; longlong n, MOD;
longlongmodpow(longlong n, longlong x, longlong MOD){ if (x < 0) { returnmodpow(modpow(n, -x, MOD), MOD - 2, MOD); } n %= MOD; longlong res = 1; while (x) { if (x & 1) { res = res * n % MOD; } n = n * n % MOD; x >>= 1; } return res; }
Combination(longlong _n, longlong MOD) : n(_n + 1), MOD(MOD) { fac = inv = vector<longlong>(n, 1); for (longlong i = 1; i < n; ++i) { fac[i] = fac[i - 1] * i % MOD; } inv[n - 1] = modpow(fac[n - 1], MOD - 2, MOD); for (longlong i = n - 2; i >= 1; --i) { inv[i] = inv[i + 1] * (i + 1) % MOD; } }
longlongfact(longlong n){ return fac[n]; }
longlongnCr(longlong n, longlong r){ if (n < r || n < 0 || r < 0) return0; return fac[n] * inv[r] % MOD * inv[n - r] % MOD; } };
longlongmodpow(longlong n, longlong x, longlong MOD){ if (x < 0) { returnmodpow(modpow(n, -x, MOD), MOD - 2, MOD); } n %= MOD; longlong res = 1; while (x) { if (x & 1) { res = res * n % MOD; } n = n * n % MOD; x >>= 1; } return res; }
constexprlonglong MOD = 1e9 + 7; Combination comb(1e5, MOD);
classSolution { public: intcountGoodArrays(int n, int m, int k){ if (m == 1) return k == n - 1; return m * modpow(m - 1, n - 1 - k, MOD) % MOD * comb.nCr(n - 1, n - 1 - k) % MOD; } };