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 long double pi = acos(-1);
  const long long Mod = 1000000007LL;
  const int N = 1e5 + 10;
  const long long Inf = 2242545357980376863LL;
  const int Log = 20;
  long long mul(long long a, long long b) { return (a * b) % Mod; }
  long long bin_pow(long long b, long long p) {
    long long res = 1;
    for (long long j = 1, pw = b; j <= p; j <<= 1, pw = mul(pw, pw))
      if (p & j) res = mul(res, pw);
    return res;
  }
  long long inv(long long x) { return bin_pow(x, Mod - 2); }
  long long f[N];
  long long fc[N];
  long long E[N], cz[N];
  long long poly[N], E2[N];
  int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    fc[0] = 1;
    for (int i = 1; i < N; i++) fc[i] = mul(fc[i - 1], i);
    long long n, k;
    cin >> n >> k;
    long long ik = inv(k);
    long long ia = inv(mul(n, k));
    long long C = 0;
    for (int i = 0; i < n; i++) {
      long long no = mul((i + (n - i) * (k - 1)) % Mod, ia);
      long long dec = mul(mul(i, (k - 1)), ia);
      long long con = 1;
      long long z = no + dec;
      if (i) con += mul(dec, f[i - 1]);
      f[i] = mul(con, inv(1 - z));
      (C += f[i]) %= Mod;
    }
    E[n] = 0;
    for (int i = n - 1; i >= 0; i--) E[i] = (f[i] + E[i + 1]) % Mod;
    map<int, long long> mp;
    int v;
    cz[0] = k;
    for (int i = 0; i < n; i++) {
      cin >> v;
      mp[v]++;
    }
    long long c = mp[-1];
    long long den = inv(bin_pow(k, c));
    for (int i = 0; i <= c; i++)
      poly[i] =
          mul(mul(bin_pow(k - 1, c - i), mul(fc[c], inv(mul(fc[i], fc[c - i])))),
              den);
    for (auto [val, cnt] : mp) {
      if (val == -1) continue;
      cz[0]--;
      cz[cnt]++;
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      if (cz[i] == 0) continue;
      for (int j = 0; j <= c; j++) (E2[i + j] += mul(cz[i], poly[j])) %= Mod;
    }
    for (int i = 0; i <= n; i++) (ans += mul(E[i], E2[i])) %= Mod;
    ans -= mul(k - 1, C);
    ans %= Mod;
    ans = mul(ans, ik);
    ans += Mod;
    cout << ans % Mod << '\n';
    return 0;
  }
  