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;
  void per() { cerr << endl; }
  template <typename Head, typename... Tail>
  void per(Head H, Tail... T) {
    cerr << H << ' ';
    per(T...);
  }
  template <class T>
  bool uin(T& a, T b) {
    return a > b ? (a = b, true) : false;
  }
  template <class T>
  bool uax(T& a, T b) {
    return a < b ? (a = b, true) : false;
  }
  template <class U, class V>
  ostream& operator<<(ostream& out, const pair<U, V>& a) {
    return out << "(" << a.first << ", " << a.second << ")";
  }
  template <class U, class V>
  istream& operator>>(istream& in, pair<U, V>& a) {
    return in >> a.first >> a.second;
  }
  template <typename W,
            typename T = typename enable_if<!is_same<W, string>::value,
                                            typename W::value_type>::type>
  ostream& operator<<(ostream& out, const W& v) {
    out << "{ ";
    for (const auto& first : v) out << first << ", ";
    return out << '}';
  }
  template <class T>
  void readArr(T from, T to) {
    for (auto i = from; i != to; ++i) cin >> *i;
  }
  mt19937 mrand(1337);
  unsigned int myRand32() { return mrand() & (unsigned int)(-1); }
  unsigned long long myRand64() {
    return ((unsigned long long)myRand32() << 32) ^ myRand32();
  }
  const int mod = 1000000007;
  void add(int& a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
  }
  void dec(int& a, int b) {
    a -= b;
    if (a < 0) a += mod;
  }
  int mult(int a, int b) { return a * (long long)b % mod; }
  int bp(int a, int b) {
    int res = 1;
    while (b > 0) {
      if (b & 1) res = mult(res, a);
      a = mult(a, a);
      b >>= 1;
    }
    return res;
  }
  int n, k;
  vector<pair<int, int> > g[200007];
  int a[200007];
  const int MAXN = 2e5 + 7;
  const size_t MAX_BIT = 18;
  const size_t MAX_SIZE = 1 << MAX_BIT;
  struct FenwickTree1 {
    int t[MAX_SIZE];
    int get(int v) {
      int res = 0;
      for (; v >= 0; v = (v & (v + 1)) - 1) {
        add(res, t[v]);
      }
      return res;
    }
    int get(int vl, int vr) {
      if (vl > vr) return 0;
      int res = get(vr);
      if (vl > 0) {
        dec(res, get(vl - 1));
      }
      return res;
    }
    void up(int v, int val) {
      for (; v < MAX_SIZE; v = v | (v + 1)) {
        add(t[v], val);
      }
    }
  } F[2], G[2];
  int RES = 0;
  class CentroidDecomposition {
   public:
    int n;
    int w[MAXN];
    int del[MAXN];
    void CalcW(int v, int p = -1) {
      w[v] = 1;
      for (auto [to, _] : g[v])
        if (to != p && !del[to]) {
          CalcW(to, v);
          w[v] += w[to];
        }
    }
    int FindCentroid(int v, int total_w, int p = -1) {
      for (auto [to, _] : g[v])
        if (to != p && !del[to] && 2 * w[to] > total_w) {
          return FindCentroid(to, total_w, v);
        }
      return v;
    }
    void col(int v, int p, int first, int last, int co, int zn, int sum) {
      add(sum, a[v]);
      F[first].up(co, zn * sum);
      G[first].up(co, zn);
      for (auto [to, be] : g[v])
        if (to != p && !del[to]) {
          col(to, v, first, be, co + (be != last), zn, sum);
        }
    }
    void go(int v, int p, int first, int last, int co, int sum) {
      if (co > k) return;
      add(sum, a[v]);
      add(RES, F[first].get(0, k - co));
      add(RES, F[first ^ 1].get(0, k - co - 1));
      add(RES, mult(sum, G[first].get(0, k - co)));
      add(RES, mult(sum, G[first ^ 1].get(0, k - co - 1)));
      for (auto [to, be] : g[v])
        if (to != p && !del[to]) {
          go(to, v, first, be, co + (be != last), sum);
        }
    }
    int Build(int v) {
      CalcW(v);
      int c = FindCentroid(v, w[v]);
      for (auto [to, be] : g[c])
        if (!del[to]) {
          col(to, c, be, be, 0, 1, 0);
        }
      add(RES, a[c]);
      add(RES, F[0].get(0, k));
      add(RES, F[1].get(0, k));
      add(RES, mult(a[c], G[0].get(0, k)));
      add(RES, mult(a[c], G[1].get(0, k)));
      for (auto [to, be] : g[c])
        if (!del[to]) {
          col(to, c, be, be, 0, -1, 0);
          go(to, c, be, be, 0, a[c]);
        }
      del[c] = 1;
      for (auto [to, _] : g[c])
        if (!del[to]) {
          Build(to);
        }
      return c;
    }
  } KEK;
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    readArr(a, a + n);
    for (int i = 0; i < n - 1; ++i) {
      int w1, w2, w3;
      cin >> w1 >> w2 >> w3;
      --w1;
      --w2;
      g[w1].push_back(make_pair(w2, w3));
      g[w2].push_back(make_pair(w1, w3));
    }
    KEK.Build(0);
    cout << RES;
  }
  