#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int head[N], to[N << 1], nex[N << 1], value[N << 1], cnt = 1;
int a[N], n, k, ans;
int sz[N], msz[N], vis[N], dis[N], cost[N], stk[N], top, root, sum;
void Add(int x, int y, int w) {
  to[cnt] = y;
  nex[cnt] = head[x];
  value[cnt] = w;
  head[x] = cnt++;
}
inline int add(int x, int y) { return x + y < mod ? x + y : x + y - mod; }
inline int sub(int x, int y) { return x >= y ? x - y : x - y + mod; }
struct Res {
  int sum[N], cnt[N];
  inline int lowbit(int x) { return x & (-x); }
  void update(int x, int v, int 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<int, int> query(int x) {
    int 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(int rt, int fa) {
  sz[rt] = 1, msz[rt] = 0;
  for (int 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(int rt, int fa, int pre) {
  stk[++top] = rt;
  for (int 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(int rt) {
  top = 0;
  vector<pair<int, int> > vt;
  for (int i = head[rt]; i; i = nex[i]) {
    if (vis[to[i]]) {
      continue;
    }
    int l = top + 1;
    dis[to[i]] = 0, cost[to[i]] = a[to[i]];
    get_dis(to[i], rt, value[i]);
    int r = top;
    if (value[i] == 0) {
      for (int j = l; j <= r; j++) {
        if (k - dis[stk[j]] >= 0) {
          int need = k - dis[stk[j]];
          pair<int, int> 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) {
          int need = k - dis[stk[j]] - 1;
          pair<int, int> cur = B.query(need + 1);
          ans = add(add(ans, cur.first),
                    1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
        }
      }
    } else {
      for (int j = l; j <= r; j++) {
        if (k - dis[stk[j]] - 1 >= 0) {
          int need = k - dis[stk[j]] - 1;
          pair<int, int> 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) {
          int need = k - dis[stk[j]];
          pair<int, int> cur = B.query(need + 1);
          ans = add(add(ans, cur.first),
                    1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
        }
      }
    }
    for (int 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});
  }
  int p = 0;
  for (int i = head[rt]; i; i = nex[i]) {
    if (vis[to[i]]) {
      continue;
    }
    int l = vt[p].first, r = vt[p].second;
    p++;
    for (int 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(int rt) {
  vis[rt] = 1;
  calc(rt);
  for (int 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);
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1, u, v, w; i < n; i++) {
    scanf("%d %d %d", &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 (int i = 1; i <= n; i++) {
    ans = add(ans, a[i]);
  }
  printf("%d\n", ans);
  return 0;
}

### Prompt

Your task is to create a cpp solution to the following problem:

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int head[N], to[N << 1], nex[N << 1], value[N << 1], cnt = 1;
int a[N], n, k, ans;
int sz[N], msz[N], vis[N], dis[N], cost[N], stk[N], top, root, sum;
void Add(int x, int y, int w) {
  to[cnt] = y;
  nex[cnt] = head[x];
  value[cnt] = w;
  head[x] = cnt++;
}
inline int add(int x, int y) { return x + y < mod ? x + y : x + y - mod; }
inline int sub(int x, int y) { return x >= y ? x - y : x - y + mod; }
struct Res {
  int sum[N], cnt[N];
  inline int lowbit(int x) { return x & (-x); }
  void update(int x, int v, int 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<int, int> query(int x) {
    int 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(int rt, int fa) {
  sz[rt] = 1, msz[rt] = 0;
  for (int 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(int rt, int fa, int pre) {
  stk[++top] = rt;
  for (int 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(int rt) {
  top = 0;
  vector<pair<int, int> > vt;
  for (int i = head[rt]; i; i = nex[i]) {
    if (vis[to[i]]) {
      continue;
    }
    int l = top + 1;
    dis[to[i]] = 0, cost[to[i]] = a[to[i]];
    get_dis(to[i], rt, value[i]);
    int r = top;
    if (value[i] == 0) {
      for (int j = l; j <= r; j++) {
        if (k - dis[stk[j]] >= 0) {
          int need = k - dis[stk[j]];
          pair<int, int> 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) {
          int need = k - dis[stk[j]] - 1;
          pair<int, int> cur = B.query(need + 1);
          ans = add(add(ans, cur.first),
                    1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
        }
      }
    } else {
      for (int j = l; j <= r; j++) {
        if (k - dis[stk[j]] - 1 >= 0) {
          int need = k - dis[stk[j]] - 1;
          pair<int, int> 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) {
          int need = k - dis[stk[j]];
          pair<int, int> cur = B.query(need + 1);
          ans = add(add(ans, cur.first),
                    1ll * add(cost[stk[j]], a[rt]) * cur.second % mod);
        }
      }
    }
    for (int 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});
  }
  int p = 0;
  for (int i = head[rt]; i; i = nex[i]) {
    if (vis[to[i]]) {
      continue;
    }
    int l = vt[p].first, r = vt[p].second;
    p++;
    for (int 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(int rt) {
  vis[rt] = 1;
  calc(rt);
  for (int 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);
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1, u, v, w; i < n; i++) {
    scanf("%d %d %d", &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 (int i = 1; i <= n; i++) {
    ans = add(ans, a[i]);
  }
  printf("%d\n", ans);
  return 0;
}

```