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 N = 2e5 + 10, mod = 1e9 + 7;
  long long head[N], to[N << 1], nex[N << 1], value[N << 1], cnt = 1;
  long long a[N], n, k, ans;
  long long sz[N], msz[N], vis[N], dis[N], cost[N], stk[N], top, root, sum;
  void Add(long long x, long long y, long long w) {
    to[cnt] = y;
    nex[cnt] = head[x];
    value[cnt] = w;
    head[x] = cnt++;
  }
  inline long long add(long long x, long long y) {
    return x + y < mod ? x + y : x + y - mod;
  }
  inline long long sub(long long x, long long y) {
    return x >= y ? x - y : x - y + mod;
  }
  struct Res {
    long long sum[N], cnt[N];
    inline long long lowbit(long long x) { return x & (-x); }
    void update(long long x, long long v, long long op) {
      while (x <= n) {
        if (op == 1) {
          sum[x] = add(sum[x], v);
          cnt[x] += 1;
        } else {
          sum[x] = sub(sum[x], v);
          cnt[x] -= 1;
        }
        x += lowbit(x);
      }
    }
    pair<long long, long long> query(long long x) {
      long long ans = 0, res = 0;
      while (x) {
        ans = add(ans, sum[x]);
        res += cnt[x];
        x -= lowbit(x);
      }
      return {ans, res};
    }
  } A, B;
  void get_root(long long rt, long long fa) {
    sz[rt] = 1, msz[rt] = 0;
    for (long long i = head[rt]; i; i = nex[i]) {
      if (vis[to[i]] || to[i] == fa) {
        continue;
      }
      get_root(to[i], rt);
      msz[rt] = max(msz[rt], sz[to[i]]);
      sz[rt] += sz[to[i]];
    }
    msz[rt] = max(msz[rt], sum - sz[rt]);
    if (msz[rt] < msz[root]) {
      root = rt;
    }
  }
  void get_dis(long long rt, long long fa, long long pre) {
    stk[++top] = rt;
    for (long long i = head[rt]; i; i = nex[i]) {
      if (to[i] == fa || vis[to[i]]) {
        continue;
      }
      dis[to[i]] = dis[rt] + (pre != value[i]),
      cost[to[i]] = add(cost[rt], a[to[i]]);
      get_dis(to[i], rt, value[i]);
    }
  }
  void calc(long long rt) {
    top = 0;
    vector<pair<long long, long long> > vt;
    for (long long i = head[rt]; i; i = nex[i]) {
      if (vis[to[i]]) {
        continue;
      }
      long long l = top + 1;
      dis[to[i]] = 0, cost[to[i]] = a[to[i]];
      get_dis(to[i], rt, value[i]);
      long long r = top;
      if (value[i] == 0) {
        for (long long j = l; j <= r; j++) {
          if (k - dis[stk[j]] >= 0) {
            long long need = k - dis[stk[j]];
            pair<long long, long long> cur = A.query(need + 1);
            ans = add(add(ans, cur.first),
                      1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
          }
          if (k - dis[stk[j]] - 1 >= 0) {
            long long need = k - dis[stk[j]] - 1;
            pair<long long, long long> cur = B.query(need + 1);
            ans = add(add(ans, cur.first),
                      1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
          }
        }
      } else {
        for (long long j = l; j <= r; j++) {
          if (k - dis[stk[j]] - 1 >= 0) {
            long long need = k - dis[stk[j]] - 1;
            pair<long long, long long> cur = A.query(need + 1);
            ans = add(add(ans, cur.first),
                      1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
          }
          if (k - dis[stk[j]] >= 0) {
            long long need = k - dis[stk[j]];
            pair<long long, long long> cur = B.query(need + 1);
            ans = add(add(ans, cur.first),
                      1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
          }
        }
      }
      for (long long j = l; j <= r; j++) {
        if (dis[stk[j]] <= k) {
          ans = add(ans, add(cost[stk[j]], a[rt]));
        }
        if (value[i] == 0) {
          A.update(dis[stk[j]] + 1, cost[stk[j]], 1);
        } else {
          B.update(dis[stk[j]] + 1, cost[stk[j]], 1);
        }
      }
      vt.push_back({l, r});
    }
    long long p = 0;
    for (long long i = head[rt]; i; i = nex[i]) {
      if (vis[to[i]]) {
        continue;
      }
      long long l = vt[p].first, r = vt[p].second;
      p++;
      for (long long j = l; j <= r; j++) {
        if (value[i] == 0) {
          A.update(dis[stk[j]] + 1, cost[stk[j]], -1);
        } else {
          B.update(dis[stk[j]] + 1, cost[stk[j]], -1);
        }
      }
    }
  }
  void solve(long long rt) {
    vis[rt] = 1;
    calc(rt);
    for (long long i = head[rt]; i; i = nex[i]) {
      if (vis[to[i]]) {
        continue;
      }
      root = 0, sum = sz[to[i]];
      get_root(to[i], rt);
      solve(root);
    }
  }
  signed main() {
    cin >> n >> k;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
    }
    for (long long i = 1, u, v, w; i < n; i++) {
      cin >> u >> v >> w;
      Add(u, v, w);
      Add(v, u, w);
    }
    sum = n, root = 0, msz[0] = 0x3f3f3f3f;
    get_root(1, 0);
    solve(root);
    for (long long i = 1; i <= n; i++) {
      ans = add(ans, a[i]);
    }
    cout << ans << "\n";
    return 0;
  }
  