#include <bits/stdc++.h>
using namespace std;
const long long mm = 1e9 + 7;
const int maxn = 1000500;
int n, q, x, y, z, tot, idx;
struct edge {
  int obj, pre, c;
} e[maxn];
int top[maxn], head[maxn], sz[maxn], a[maxn], dep[maxn], fa[maxn], son[maxn],
    up[maxn], dw[maxn];
long long fac[maxn], ans;
void insert(int x, int y, int z) {
  e[++tot].obj = y;
  e[tot].pre = head[x];
  head[x] = tot;
  e[tot].c = z;
}
void dfs(int u) {
  sz[u] = 1;
  for (int j = head[u]; j; j = e[j].pre) {
    int v = e[j].obj;
    if (v == fa[u]) continue;
    dep[v] = dep[u] + 1;
    fa[v] = u;
    a[v] = e[j].c;
    dfs(v);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
void make(int u, int ff) {
  top[u] = ff;
  if (son[u]) make(son[u], ff);
  for (int j = head[u]; j; j = e[j].pre) {
    int v = e[j].obj;
    if (v == fa[u] || v == son[u]) continue;
    make(v, v);
  }
}
int lca(int x, int y) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    x = fa[top[x]];
  }
  if (dep[x] > dep[y]) return y;
  return x;
}
void solve(int u) {
  for (int j = head[u]; j; j = e[j].pre) {
    int v = e[j].obj;
    if (v == fa[u]) continue;
    solve(v);
    up[u] += up[v];
    dw[u] += dw[v];
  }
  if (a[u] == 1)
    ans = (ans + fac[up[u]] - 1) % mm;
  else if (a[u] == -1)
    ans = (ans + fac[dw[u]] - 1) % mm;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= 1000001; i++) fac[i] = fac[i - 1] * 2 % mm;
  scanf("%d", &n);
  for (int i = 2; i <= n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    insert(x, y, z);
    insert(y, x, -z);
  }
  dfs(1);
  make(1, 1);
  scanf("%d", &q);
  x = 1;
  while (q--) {
    scanf("%d", &y);
    int t = lca(x, y);
    up[x]++;
    up[t]--;
    dw[y]++;
    dw[t]--;
    x = y;
  }
  solve(1);
  printf("%lld\n", ans);
  return 0;
}
