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;
  template <class T>
  ostream& operator<<(ostream& out, vector<T> A) {
    for (auto I : A) out << I << " ";
    return out;
  }
  void debug_out() { cerr << endl; }
  template <typename Head, typename... Tail>
  void debug_out(Head H, Tail... T) {
    cerr << ' ' << H;
    debug_out(T...);
  }
  const int N = 2e5 + 5, mod = 1e9 + 7;
  vector<pair<int, int> > E[N];
  int n, k, val[N], ans, c[2][N], d[2][N], L[N], R[N], dft, re[N], cost[N], V[N];
  void add(int c[], int i, int x) {
    for (; i <= n; i += i & -i) {
      c[i] += x;
      if (c[i] >= mod) c[i] -= mod;
      if (c[i] < 0) c[i] += mod;
    }
  }
  int query(int c[], int i) {
    int s = 0;
    for (; i > 0; i -= i & -i) {
      s += c[i];
      if (s >= mod) s -= mod;
    }
    return s;
  }
  namespace Divide {
  int dp[N], S, F[N], root, vis[N], siz[N];
  void init(int n) { S = n, root = 0, dp[0] = 1e9; }
  void getroot(int u, int pre) {
    siz[u] = 1, dp[u] = 0;
    for (auto& e : E[u]) {
      int v, w;
      tie(v, w) = e;
      if (v == pre || vis[v]) continue;
      getroot(v, u);
      siz[u] += siz[v];
      dp[u] = max(dp[u], siz[v]);
    }
    dp[u] = max(dp[u], S - siz[u]);
    if (dp[root] > dp[u]) root = u;
  }
  void dfs(int u, int pre, int las) {
    L[u] = ++dft, re[dft] = u;
    for (auto& e : E[u]) {
      int v, w;
      tie(v, w) = e;
      if (vis[v] || v == pre) continue;
      cost[v] = cost[u] + (las != w);
      V[v] = V[u] + val[v];
      if (V[v] >= mod) V[v] -= mod;
      dfs(v, u, w);
    }
    R[u] = dft;
  }
  void solve(int u) {
    cost[u] = dft = 0;
    V[u] = val[u];
    dfs(u, 0, -1);
    for (auto& e : E[u]) {
      int v, w;
      tie(v, w) = e;
      if (vis[v]) continue;
      for (int i = L[v]; i <= R[v]; i++) {
        int need = k - cost[re[i]];
        if (need >= 0) {
          ans += V[re[i]];
          if (ans >= mod) ans -= mod;
        }
        ans += query(c[w], need + 1);
        if (ans >= mod) ans -= mod;
        ans += (long long)query(d[w], need + 1) * V[re[i]] % mod;
        if (ans >= mod) ans -= mod;
        ans -= (long long)query(d[w], need + 1) * val[u] % mod;
        if (ans < 0) ans += mod;
        ans += query(c[w ^ 1], need);
        if (ans >= mod) ans -= mod;
        ans += (long long)query(d[w ^ 1], need) * V[re[i]] % mod;
        if (ans >= mod) ans -= mod;
        ans -= (long long)query(d[w ^ 1], need) * val[u] % mod;
        if (ans < 0) ans += mod;
      }
      for (int i = L[v]; i <= R[v]; i++)
        add(c[w], cost[re[i]], V[re[i]]), add(d[w], cost[re[i]], 1);
    }
    for (auto& e : E[u]) {
      int v, w;
      tie(v, w) = e;
      if (vis[v]) continue;
      for (int i = L[v]; i <= R[v]; i++)
        add(c[w], cost[re[i]], -V[re[i]]), add(d[w], cost[re[i]], -1);
    }
  }
  void dfz(int u) {
    int now = S;
    vis[u] = 1;
    solve(u);
    for (auto& e : E[u]) {
      int v, w;
      tie(v, w) = e;
      if (vis[v]) continue;
      S = siz[v] > siz[u] ? now - siz[u] : siz[v];
      init(S);
      getroot(v, u);
      dfz(root);
    }
  }
  }  // namespace Divide
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> k;
    k++;
    for (int i = 1; i <= n; i++) cin >> val[i], ans = (ans + val[i]) % mod;
    for (int i = 1, u, v, w; i < n; i++) {
      cin >> u >> v >> w;
      E[u].emplace_back(v, w);
      E[v].emplace_back(u, w);
    }
    Divide::init(n);
    Divide::getroot(1, 0);
    Divide::dfz(Divide::root);
    cout << ans << '\n';
    return 0;
  }
  