description: There is a city park represented as a tree with n attractions as its vertices and n - 1 rails as its edges. The i-th attraction has happiness value a_i.
  
  Each rail has a color. It is either black if t_i = 0, or white if t_i = 1. Black trains only operate on a black rail track, and white trains only operate on a white rail track. If you are previously on a black train and want to ride a white train, or you are previously on a white train and want to ride a black train, you need to use 1 ticket.
  
  The path of a tour must be a simple path — it must not visit an attraction more than once. You do not need a ticket the first time you board a train. You only have k tickets, meaning you can only switch train types at most k times. In particular, you do not need a ticket to go through a path consisting of one rail color.
  
  Define f(u, v) as the sum of happiness values of the attractions in the tour (u, v), which is a simple path that starts at the u-th attraction and ends at the v-th attraction. Find the sum of f(u,v) for all valid tours (u, v) (1 ≤ u ≤ v ≤ n) that does not need more than k tickets, modulo 10^9 + 7.
  
  Input
  
  The first line contains two integers n and k (2 ≤ n ≤ 2 ⋅ 10^5, 0 ≤ k ≤ n-1) — the number of attractions in the city park and the number of tickets you have.
  
  The second line contains n integers a_1, a_2,…, a_n (0 ≤ a_i ≤ 10^9) — the happiness value of each attraction.
  
  The i-th of the next n - 1 lines contains three integers u_i, v_i, and t_i (1 ≤ u_i, v_i ≤ n, 0 ≤ t_i ≤ 1) — an edge between vertices u_i and v_i with color t_i. The given edges form a tree.
  
  Output
  
  Output an integer denoting the total happiness value for all valid tours (u, v) (1 ≤ u ≤ v ≤ n), modulo 10^9 + 7.
  
  Examples
  
  Input
  
  
  5 0
  1 3 2 6 4
  1 2 1
  1 4 0
  3 2 1
  2 5 0
  
  
  Output
  
  
  45
  
  
  Input
  
  
  3 1
  1 1 1
  1 2 1
  3 2 0
  
  
  Output
  
  
  10
solution: #include <bits/stdc++.h>
  using namespace std;
  long long n, k;
  long long a[212121];
  vector<pair<long long, long long>> edge[212121];
  long long ans = 0, mod = 1e9 + 7;
  long long is_cent[212121];
  long long S[212121], cent;
  void mk_s(long long w, long long bef) {
    S[w] = 1;
    for (auto [nxt, ncol] : edge[w]) {
      if (is_cent[nxt]) continue;
      if (nxt == bef) continue;
      mk_s(nxt, w);
      S[w] += S[nxt];
    }
  }
  long long get_cent(long long w, long long bef, long long all) {
    for (auto [nxt, ncol] : edge[w]) {
      if (is_cent[nxt]) continue;
      if (nxt == bef) continue;
      if (S[nxt] > all / 2) return get_cent(nxt, w, all);
    }
    return w;
  }
  vector<pair<long long, long long>> G[212121];
  vector<long long> cnt[212121], SUM[212121];
  void mk_G(long long w, long long bef, long long bef_col, long long sum,
            long long trans, long long rep) {
    G[rep].push_back({sum, trans});
    for (auto [nxt, ncol] : edge[w]) {
      if (is_cent[nxt]) continue;
      if (nxt == bef) continue;
      mk_G(nxt, w, ncol, (sum + a[nxt]) % mod, trans + (ncol != bef_col), rep);
    }
  }
  void decomp(long long w) {
    mk_s(w, -1);
    long long cent = get_cent(w, -1, S[w]);
    is_cent[cent] = 1;
    vector<long long> ALLcnt[2], ALLS[2];
    ALLcnt[0].resize(S[w], 0);
    ALLS[0].resize(S[w], 0);
    ALLcnt[1].resize(S[w], 0);
    ALLS[1].resize(S[w], 0);
    for (auto [nxt, ncol] : edge[cent]) {
      if (is_cent[nxt]) continue;
      G[nxt].clear();
      mk_G(nxt, cent, ncol, a[nxt], 0, nxt);
      long long mxt = 0;
      for (auto [sum, trans] : G[nxt]) {
        ALLcnt[ncol][trans]++;
        mxt = max(mxt, trans);
      }
      cnt[nxt].clear();
      cnt[nxt].resize(mxt + 1, 0);
      SUM[nxt].clear();
      SUM[nxt].resize(mxt + 1, 0);
      for (auto [sum, trans] : G[nxt]) cnt[nxt][trans]++;
      for (long long i = 0; i <= mxt; i++) {
        SUM[nxt][i] += cnt[nxt][i];
        if (i > 0) SUM[nxt][i] += SUM[nxt][i - 1];
      }
    }
    for (long long i = 0; i < ALLcnt[0].size(); i++) {
      ALLS[0][i] = ALLcnt[0][i] + (i > 0 ? ALLS[0][i - 1] : 0);
      ALLS[1][i] = ALLcnt[1][i] + (i > 0 ? ALLS[1][i - 1] : 0);
    }
    long long allcount = 0;
    for (auto [nxt, ncol] : edge[cent]) {
      if (is_cent[nxt]) continue;
      for (auto [sum, trans] : G[nxt]) {
        if (k - trans >= 0) {
          long long SnxtIdx = k - trans;
          long long ncolIdx = k - trans;
          if (SnxtIdx >= SUM[nxt].size()) SnxtIdx = SUM[nxt].size() - 1;
          if (ncolIdx >= ALLS[ncol].size()) ncolIdx = ALLS[ncol].size() - 1;
          long long gaesu = ALLS[ncol][ncolIdx] - SUM[nxt][SnxtIdx];
          allcount += gaesu;
          ans += sum * (gaesu % mod) % mod;
          ans %= mod;
        }
        if (k - trans - 1 >= 0) {
          long long ncolIdx = k - trans - 1;
          if (ncolIdx >= ALLS[ncol].size()) ncolIdx = ALLS[ncol].size() - 1;
          long long gaesu = ALLS[!ncol][ncolIdx];
          allcount += gaesu;
          ans += sum * (gaesu % mod) % mod;
          ans %= mod;
        }
      }
    }
    allcount /= 2;
    allcount %= mod;
    allcount++;
    for (auto [nxt, ncol] : edge[cent]) {
      if (is_cent[nxt]) continue;
      for (auto [sum, trans] : G[nxt]) {
        if (trans <= k) {
          ans = (ans + sum) % mod;
          allcount++;
        }
      }
    }
    ans += allcount * a[cent];
    ans %= mod;
    for (auto [nxt, ncol] : edge[cent]) {
      if (is_cent[nxt]) continue;
      decomp(nxt);
    }
  }
  int main() {
    long long i, j;
    scanf("%lld%lld", &n, &k);
    for (i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (i = 0; i < n - 1; i++) {
      long long s, e, col;
      scanf("%lld%lld%lld", &s, &e, &col);
      edge[s].push_back({e, col});
      edge[e].push_back({s, col});
    }
    decomp(1);
    printf("%lld\n", ans % mod);
    return 0;
  }
  