#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 10, LG = 20;
const long long MOD = 1e9 + 7;
int n;
long long ans = 1, fac[MAXN];
int par[MAXN][LG], h[MAXN], ps1[MAXN], ps2[MAXN];
int nxt[MAXN], pre[MAXN];
bool mark[MAXN];
vector<int> adj[MAXN];
vector<pair<int, int> > tartib[MAXN];
void dfs(int u) {
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (v == par[u][0]) continue;
    par[v][0] = u, h[v] = h[u] + 1;
    for (int i = 1; i < LG; i++) par[v][i] = par[par[v][i - 1]][i - 1];
    dfs(v);
  }
  return;
}
void dfs2(int u) {
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (v == par[u][0]) continue;
    dfs2(v);
    ps1[u] += ps1[v], ps2[u] += ps2[v];
  }
  if (ps1[u]) tartib[par[u][0]].push_back({u, par[u][1]});
  if (ps2[u]) tartib[par[u][0]].push_back({par[u][1], u});
  return;
}
inline int go_to_par(int u, int d) {
  for (int i = LG - 1; i >= 0; i--)
    if ((d >> i) & 1) u = par[u][i];
  return u;
}
inline int lca(int u, int v) {
  if (h[u] < h[v]) swap(u, v);
  u = go_to_par(u, h[u] - h[v]);
  if (u == v) return u;
  for (int i = LG - 1; i >= 0; i--)
    if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
  return par[u][0];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % MOD;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v, u--, v--;
    adj[u].push_back(v), adj[v].push_back(u);
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    int label;
    cin >> label, label--;
    if (label == -1) continue;
    int lc = lca(i, label);
    int afl = -1, afi = -1;
    if (label != lc) {
      afl = go_to_par(label, h[label] - h[lc] - 1);
      ps1[label]++, ps1[afl]--;
      tartib[label].push_back({n, par[label][0]});
    }
    if (i != lc) {
      afi = go_to_par(i, h[i] - h[lc] - 1);
      ps2[i]++, ps2[afi]--;
      tartib[i].push_back({par[i][0], n + 1});
    } else
      tartib[i].push_back({afl, n + 1});
    if (label == lc) tartib[label].push_back({n, afi});
    if (lc != i && lc != label) tartib[lc].push_back({afl, afi});
  }
  dfs2(0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < adj[i].size(); j++) {
      int v = adj[i][j];
      nxt[v] = -1, pre[v] = -1;
    }
    nxt[n] = nxt[n + 1] = pre[n] = pre[n + 1] = -1;
    for (int j = 0; j < tartib[i].size(); j++) {
      int f = tartib[i][j].first, s = tartib[i][j].second;
      if (nxt[f] == -1) nxt[f] = s;
      if (pre[s] == -1) pre[s] = f;
      if (nxt[f] != s || pre[s] != f) return cout << 0 << endl, 0;
    }
    adj[i].push_back(n), adj[i].push_back(n + 1);
    for (int j = 0; j < adj[i].size(); j++) mark[adj[i][j]] = false;
    bool bad = false;
    int cnt = 0;
    for (int j = 0; j < adj[i].size(); j++) {
      int v = adj[i][j];
      if (mark[v] || pre[v] != -1) {
        continue;
      }
      mark[v] = true;
      int cur = v;
      while (nxt[cur] != -1) cur = nxt[cur], mark[cur] = true;
      if (cur == n + 1 && v == n) bad = true;
      cnt++;
    }
    for (int j = 0; j < adj[i].size(); j++)
      if (!mark[adj[i][j]]) return cout << 0 << endl, 0;
    if (cnt != 1 && bad) return cout << 0 << endl, 0;
    if (cnt != 1) ans = ans * fac[cnt - 2] % MOD;
  }
  cout << ans << endl;
  return 0;
}
