#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5, Mod = 1e9 + 7;
struct Edge {
  int nxt, to;
} e[maxn << 1];
int hd[maxn], e_cnt = 1;
void add(int u, int v) {
  e[++e_cnt] = (Edge){hd[u], v};
  hd[u] = e_cnt;
}
int n, pre[maxn << 2], nxt[maxn << 2], p[maxn], dep[maxn], fac[maxn];
bool vis[maxn << 2];
void dfs(int u) {
  for (int i = hd[u]; i; i = e[i].nxt)
    if (i != p[u]) p[e[i].to] = i ^ 1, dep[e[i].to] = dep[u] + 1, dfs(e[i].to);
}
vector<int> getRoute(int u, int v) {
  vector<int> v1, v2;
  while (dep[u] != dep[v]) {
    if (dep[u] > dep[v])
      v1.push_back(p[u]), u = e[p[u]].to;
    else
      v2.push_back(p[v]), v = e[p[v]].to;
  }
  while (u != v)
    v1.push_back(p[u]), v2.push_back(p[v]), u = e[p[u]].to, v = e[p[v]].to;
  int siz = v2.size();
  for (int i = siz - 1; i >= 0; i--) v1.push_back(v2[i] ^ 1);
  return v1;
}
void link(int a, int b) {
  if (nxt[a] || pre[b]) {
    cout << 0;
    exit(0);
  }
  nxt[a] = b;
  pre[b] = a;
}
void cover(int u) {
  vis[u] = true;
  if (nxt[u]) cover(nxt[u]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fac[0] = 1;
  for (int i = 1; i <= 5e5; i++) fac[i] = (long long)fac[i - 1] * i % Mod;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    cin >> a >> b;
    add(a, b);
    add(b, a);
  }
  dfs(1);
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    if (!a) continue;
    if (i == a) {
      cout << 0;
      return 0;
    }
    vector<int> vec = getRoute(a, i);
    int siz = vec.size();
    sum += siz;
    if (sum > n * 2 - 2) {
      cout << 0;
      return 0;
    }
    link(a, vec[0] + n * 2);
    link((vec[siz - 1] ^ 1) + n * 2, i + n);
    for (int j = 0; j + 1 < siz; j++)
      link((vec[j] ^ 1) + n * 2, vec[j + 1] + n * 2);
  }
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    cover(i);
    if (vis[i + n]) {
      for (int j = hd[i]; j; j = e[j].nxt)
        if (!vis[j + n * 2]) {
          cout << 0;
          return 0;
        }
    } else {
      int cnt = 0;
      for (int j = hd[i]; j; j = e[j].nxt)
        if (!pre[j + n * 2]) cover(j + n * 2), cnt++;
      for (int j = hd[i]; j; j = e[j].nxt)
        if (!vis[j + n * 2]) {
          cout << 0;
          return 0;
        }
      if (vis[i + n]) cnt--;
      ans = (long long)ans * fac[cnt] % Mod;
    }
  }
  cout << ans << '\n';
  return 0;
}
