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>
  using namespace std;
  const int P = 1e9 + 7;
  int ad(int k1, int k2) { return k1 += k2 - P, k1 += k1 >> 31 & P; }
  int su(int k1, int k2) { return k1 -= k2, k1 += k1 >> 31 & P; }
  int mu(int k1, int k2) { return 1LL * k1 * k2 % P; }
  void uad(int& k1, int k2) { k1 += k2 - P, k1 += k1 >> 31 & P; }
  void usu(int& k1, int k2) { k1 -= k2, k1 += k1 >> 31 & P; }
  template <class... T>
  int ad(int k1, T... k2) {
    return ad(k1, ad(k2...));
  }
  template <class... T>
  int su(int k1, T... k2) {
    return su(k1, ad(k2...));
  }
  template <class... T>
  int mu(int k1, T... k2) {
    return mu(k1, mu(k2...));
  }
  template <class... T>
  void uad(int& k1, T... k2) {
    return uad(k1, ad(k2...));
  }
  template <class... T>
  void usu(int& k1, T... k2) {
    return usu(k1, ad(k2...));
  }
  int po(int k1, int k2) {
    int k3 = 1;
    for (; k2; k2 >>= 1, k1 = mu(k1, k1))
      if (k2 & 1) k3 = mu(k3, k1);
    return k3;
  }
  const int N = 100005;
  int n, K, f[N], a[N], c[N], tot, fac[N], inv[N], ifac[N];
  map<int, int> b;
  int C(int k1, int k2) {
    if (k1 < 0 || k2 < 0 || k1 - k2 < 0) return 0;
    return 1LL * fac[k1] * ifac[k2] % P * ifac[k1 - k2] % P;
  }
  int main() {
    fac[0] = fac[1] = inv[0] = inv[1] = ifac[0] = ifac[1] = 1;
    for (int i = 2; i < N; ++i)
      fac[i] = 1LL * fac[i - 1] * i % P,
      inv[i] = 1LL * (P - P / i) * inv[P % i] % P,
      ifac[i] = 1LL * ifac[i - 1] * inv[i] % P;
    scanf("%d%d", &n, &K);
    f[0] = 0;
    f[1] = 1;
    for (int x = (1); x <= (n - 1); ++x) {
      int rhs = (n + 1LL * n * K % P * f[x] - 1LL * x * (K - 1) % P * f[x - 1] -
                 (x + 1LL * (n - x) * (K - 1)) % P * f[x]) %
                P;
      if (rhs < P) rhs += P;
      f[x + 1] = mu(rhs, po(n - x, P - 2));
    }
    for (int i = (1); i <= (n); ++i) {
      scanf("%d", &a[i]);
      if (a[i] == -1)
        ++tot;
      else
        ++b[a[i]];
    }
    int ans = 0;
    for (auto x : b) {
      ++c[x.second];
    }
    c[0] += K - ((int)(b).size());
    const int invK = po(K, P - 2);
    for (int i = (0); i <= (n); ++i)
      if (c[i]) {
        for (int j = (0); j <= (tot); ++j) {
          uad(ans, mu(c[i], f[i + j], C(tot, j), po(invK, j),
                      po(su(1, invK), tot - j)));
        }
      }
    printf("%d\n", su(f[n], ans));
    return 0;
  }
  