#include <bits/stdc++.h>
using namespace std;
long long facd[500005];
void pre(int n) {
  facd[0] = 1;
  for (int i = 1; i <= n; i++) facd[i] = facd[i - 1] * i % 1000000007;
}
int d[500005], bel[1500005];
int lpos[1500005], rpos[1500005];
bool vis2[1500005], vis1[1500005];
void merge(int x, int y) {
  if (vis2[x] || vis1[y]) {
    puts("0");
    exit(0);
  }
  vis2[x] = vis1[y] = 1;
  int u = lpos[x], v = rpos[y];
  if (u == y && d[bel[x]] >= 0) {
    puts("0");
    exit(0);
  }
  rpos[u] = v;
  lpos[v] = u;
}
vector<int> e[500005];
int dep[500005], fa[500005];
int id1[500005], id2[500005], cnt;
int dfn[500005], end2[500005], dfs_cnt;
void dfs(int x) {
  d[x] = (int)e[x].size();
  dfn[x] = ++dfs_cnt;
  bel[x] = bel[id1[x]] = x;
  for (int i = 0; i < e[x].size(); i++)
    if (e[x][i] != fa[x]) {
      int u = e[x][i];
      fa[u] = x;
      dep[u] = dep[x] + 1;
      id1[u] = ++cnt;
      id2[u] = ++cnt;
      bel[id2[u]] = x;
      dfs(u);
    }
  end2[x] = dfs_cnt;
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  while (dep[x] > dep[y]) {
    d[x]--;
    x = fa[x];
  }
  while (x != y) {
    d[x]--;
    d[y]--;
    x = fa[x];
    y = fa[y];
  }
  d[x]--;
  return x;
}
pair<int, int> q[500005];
void update(int x, int y) {
  if (x == y) {
    puts("0");
    exit(0);
  }
  int nx = x, ny = y;
  int p = lca(x, y), sz = 0;
  while (x != p) {
    q[++sz] = pair<int, int>(id1[x], id2[x]);
    x = fa[x];
  }
  int last2 = sz;
  while (y != p) {
    q[++sz] = pair<int, int>(id2[y], id1[y]);
    y = fa[y];
  }
  reverse(q + last2 + 1, q + sz + 1);
  merge(nx, q[1].first);
  for (int i = 1; i < sz; i++) merge(q[i].second, q[i + 1].first);
  merge(q[sz].second, ny);
}
int main() {
  int n;
  scanf("%d", &n);
  pre(n);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
  }
  cnt = n;
  dfs(1);
  for (int i = 1; i <= cnt; i++) lpos[i] = rpos[i] = i;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (x) update(x, i);
  }
  long long ans = 1;
  for (int i = 1; i <= n; i++)
    if (d[i] >= 0) ans = ans * facd[d[i]] % 1000000007;
  printf("%lld\n", ans);
  return 0;
}
