description: Mr. Chanek opened a letter from his fellow, who is currently studying at Singanesia. Here is what it says.
  
  Define an array b (0 ≤ b_i < k) with n integers. While there exists a pair (i, j) such that b_i ≠ b_j, do the following operation:
  
    * Randomly pick a number i satisfying 0 ≤ i < n. Note that each number i has a probability of 1/n to be picked. 
    * Randomly Pick a number j satisfying 0 ≤ j < k. 
    * Change the value of b_i to j. It is possible for b_i to be changed to the same value. 
  
  
  
  Denote f(b) as the expected number of operations done to b until all elements of b are equal. 
  
  You are given two integers n and k, and an array a (-1 ≤ a_i < k) of n integers. 
  
  For every index i with a_i = -1, replace a_i with a random number j satisfying 0 ≤ j < k. Let c be the number of occurrences of -1 in a. There are k^c possibilites of a after the replacement, each with equal probability of being the final array.
  
  Find the expected value of f(a) modulo 10^9 + 7. 
  
  Formally, let M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≡ 0 \pmod{M}. Output the integer equal to p ⋅ q^{-1} mod M. In other words, output such an integer x that 0 ≤ x < M and x ⋅ q ≡ p \pmod{M}.
  
  After reading the letter, Mr. Chanek gave the task to you. Solve it for the sake of their friendship!
  
  Input
  
  The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 10^9). 
  
  The second line contains n integers a_1, a_2, …, a_n (-1 ≤ a_i < k).
  
  Output
  
  Output an integer denoting the expected value of f(a) modulo 10^9 + 7.
  
  Examples
  
  Input
  
  
  2 2
  0 1
  
  
  Output
  
  
  2
  
  
  Input
  
  
  2 2
  0 -1
  
  
  Output
  
  
  1
  
  
  Input
  
  
  3 3
  0 1 1
  
  
  Output
  
  
  12
  
  
  Input
  
  
  3 3
  -1 -1 -1
  
  
  Output
  
  
  11
  
  
  Input
  
  
  10 9
  -1 0 -1 1 1 2 2 3 3 3
  
  
  Output
  
  
  652419213
solution: #include <bits/stdc++.h>
  const int P = 1000000007, Q = P - 1;
  int mpow(int x, int k) {
    if (k == 0) return 1;
    int ret = mpow(x * (unsigned long long)x % P, k >> 1);
    if (k & 1) ret = ret * (unsigned long long)x % P;
    return ret;
  }
  const int N = 100010;
  int e[N], fac[N], ifac[N], inv[N], pp[N], pq[N];
  int cnt[N];
  int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    int n, k;
    std::cin >> n >> k;
    fac[0] = 1;
    for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * (unsigned long long)i % P;
    ifac[n] = mpow(fac[n], P - 2);
    for (int i = n; i; --i) ifac[i - 1] = ifac[i] * (unsigned long long)i % P;
    for (int i = 1; i <= n; ++i)
      inv[i] = ifac[i] * (unsigned long long)fac[i - 1] % P;
    int p = mpow(k, P - 2), in = inv[n];
    e[0] = k;
    for (int i = 1; i != n; ++i) {
      e[i] =
          (i * (unsigned long long)(k - 1) % P * p % P * in % P * e[i - 1] + 1) %
          P;
      e[i] = e[i] * (unsigned long long)n % P * k % P * inv[n - i] % P;
    }
    for (int i = n - 1; i >= 0; --i) e[i] = (e[i] + e[i + 1]) % P;
    std::unordered_map<int, int> mp;
    int oth = 0;
    for (int rep = 0; rep != n; ++rep) {
      int ai;
      std::cin >> ai;
      if (ai == -1)
        ++oth;
      else
        ++mp[ai];
    }
    int q = 1 - p;
    if (q < 0) q += P;
    pp[0] = 1;
    for (int i = 1; i <= n; ++i) pp[i] = pp[i - 1] * (unsigned long long)p % P;
    pq[0] = 1;
    for (int i = 1; i <= n; ++i) pq[i] = pq[i - 1] * (unsigned long long)q % P;
    auto cal = [&](int c) {
      int ret = 0;
      for (int i = 0; i <= oth; ++i) {
        ret = (ret + pp[i] * (unsigned long long)pq[oth - i] % P * ifac[i] % P *
                         ifac[oth - i] % P * e[c + i]) %
              P;
      }
      return ret * (unsigned long long)fac[oth] % P;
    };
    cnt[0] = k;
    for (const auto& pr : mp) {
      ++cnt[pr.second];
      --cnt[0];
    }
    int ans = (P - k + 1) * (unsigned long long)e[0] % P;
    for (int i = 0; i <= n; ++i)
      if (cnt[i]) ans = (ans + cnt[i] * (unsigned long long)cal(i)) % P;
    ans = ans * (unsigned long long)p % P;
    std::cout << ans << '\n';
    return 0;
  }
  