#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MAXN = 500 * 1000 + 2, MOD = 1000 * 1000 * 1000 + 7, L = 22;
int n, a[MAXN], dis[MAXN], par[MAXN], in[MAXN], out[MAXN], all, fact[MAXN];
vector<int> adj[MAXN];
bool mark[MAXN];
vector<pair<int, int>> p[MAXN];
inline long long add(long long a, long long b) {
  return ((a % MOD + b % MOD) % MOD + MOD) % MOD;
}
inline long long mul(long long a, long long b) {
  return (a % MOD * b % MOD) % MOD;
}
inline void dfs(int v, int p = -1) {
  par[v] = p;
  for (auto u : adj[v])
    if (u != p) {
      dis[u] = dis[v] + 1;
      dfs(u, v);
    }
  return;
}
inline void lca(int b, int e) {
  vector<int> vr, vl;
  vl.push_back(n + 2);
  vr.push_back(n + 1);
  while (dis[b] > dis[e]) {
    vl.push_back(b);
    b = par[b];
  }
  while (dis[e] > dis[b]) {
    vr.push_back(e);
    e = par[e];
  }
  while (b != e) {
    vl.push_back(b);
    vr.push_back(e);
    b = par[b];
    e = par[e];
  }
  vl.push_back(b);
  while (!vr.empty()) {
    vl.push_back(vr.back());
    vr.pop_back();
  }
  for (int i = 1; i < (int)vl.size() - 1; i++) {
    p[vl[i]].push_back({vl[i - 1], vl[i + 1]});
  }
  all += (int)vl.size() - 3;
  if (all > 2 * n - 2) {
    cout << 0 << endl;
    exit(0);
  }
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  if (n == 1) return cout << 1 << endl, 0;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v), adj[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) adj[i].push_back(n + 2), adj[i].push_back(n + 1);
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
    if (a[i] == 0) continue;
    if (a[i] == i) return cout << 0 << endl, 0;
    lca(a[i], i);
  }
  long long ans = 1;
  for (int i = 1; i < n + 1; i++) {
    for (auto e : p[i]) {
      int u = e.first, v = e.second;
      if (out[u] && out[u] != v) return cout << 0 << endl, 0;
      if (in[v] && in[v] != u) return cout << 0 << endl, 0;
      in[v] = u, out[u] = v;
    }
    for (auto u : adj[i])
      if (!mark[u]) {
        int cur = out[u];
        while (cur != 0) {
          if (cur == u) return cout << 0 << endl, 0;
          mark[cur] = 1;
          cur = out[cur];
        }
      }
    if (out[n + 2]) {
      int cur = out[n + 2], cnt = 1;
      while (cur) {
        cnt++;
        if (cur == n + 1) break;
        cur = out[cur];
      }
      if ((cur == n + 1) && (cnt < (int)adj[i].size()))
        return cout << 0 << endl, 0;
    }
    int ok = 0;
    for (int u : adj[i]) {
      if (u <= n && !in[u]) ok++;
    }
    if (in[n + 1]) ok--;
    ok = max(ok, 0);
    ans = mul(ans, fact[ok]);
    for (int u : adj[i]) {
      mark[u] = 0;
      out[u] = 0;
      in[u] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
