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;
  const long long mod = 1000000007;
  long long n, K, m, rt, a[500000], f[2][500000], g[2][500000], sz[500000],
      mx[500000], vis[500000], ans;
  vector<long long> e[500000][2];
  void add(long long u, long long fa, long long num, long long sum, long long col,
           long long fst) {
    sum += a[u];
    (f[fst][num] += sum) %= mod;
    ++g[fst][num];
    m = max(m, num);
    for (long long op : {0, 1}) {
      for (long long v : e[u][op]) {
        if (v != fa && !vis[v]) {
          add(v, u, num + (op != col), sum, op, fst);
        }
      }
    }
  }
  void upd(long long k, long long val) {
    for (long long i = 1; i <= m; ++i) {
      g[0][i] += g[0][i - 1];
      g[1][i] += g[1][i - 1];
    }
    if (val != -1) ans += (g[0][min(K, m)] + g[1][min(K, m)]) * val * k % mod;
    for (long long op : {0, 1}) {
      for (long long i = 1; i <= m; ++i) {
        ans += k * f[op][i] * g[op][min(K - i + 1, m)] % mod;
        ans += k * f[op][i] * g[op ^ 1][min(K - i, m)] % mod;
        if (i <= K && val != -1) ans += f[op][i] * k;
      }
    }
    ans %= mod;
    for (long long i = 1; i <= m; ++i) f[0][i] = f[1][i] = g[0][i] = g[1][i] = 0;
  }
  void calc(long long u, long long fa, long long num) {
    mx[u] = 0;
    for (long long op : {0, 1}) {
      for (long long v : e[u][op]) {
        if (!vis[v] && v != fa) {
          calc(v, u, num);
          mx[u] = max(mx[u], sz[v]);
        }
      }
    }
    mx[u] = max(mx[u], num - sz[u]);
    if (mx[u] < mx[rt]) rt = u;
  }
  void dfs(long long u, long long fa) {
    sz[u] = 1;
    for (long long op : {0, 1}) {
      for (long long v : e[u][op]) {
        if (!vis[v] && v != fa) {
          dfs(v, u);
          sz[u] += sz[v];
        }
      }
    }
  }
  void solve(long long x) {
    vis[x] = 1;
    m = 0;
    for (long long op : {0, 1}) {
      for (long long v : e[x][op])
        if (!vis[v]) {
          add(v, 0, 1, a[x] / 2, op, op);
        }
    }
    upd(1, a[x] / 2);
    for (long long op : {0, 1}) {
      m = 0;
      for (long long v : e[x][op])
        if (!vis[v]) {
          add(v, 0, 1, a[x] / 2, op, op);
          upd(-1, -1);
        }
    }
    for (long long op : {0, 1}) {
      for (long long v : e[x][op])
        if (!vis[v]) {
          rt = 0;
          dfs(v, 0);
          calc(v, 0, sz[v]);
          solve(rt);
        }
    }
  }
  int main() {
    scanf("%lld%lld", &n, &K);
    ++K;
    for (long long i = 1; i <= n; ++i) {
      scanf("%lld", a + i);
      a[i] *= 2;
      ans += a[i];
    }
    for (long long i = 1; i < n; ++i) {
      long long x, y, z;
      scanf("%lld%lld%lld", &x, &y, &z);
      e[x][z].push_back(y);
      e[y][z].push_back(x);
    }
    mx[rt = 0] = n + 1;
    dfs(1, 0);
    calc(1, 0, sz[1]);
    solve(rt);
    (ans += mod) %= mod;
    ans = ans * ((mod + 1) / 2) % mod;
    printf("%lld\n", ans);
    return 0;
  }
  