#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 5e5 + 10, maxm = 32e3 + 10, lg = 7, mod = 1e9 + 7,
                inf = 1e18;
long long n, fac[maxn], h[maxn], par[maxn], mk[maxn];
vector<long long> g[maxn];
map<long long, long long> l[maxn], r[maxn];
void pfs(long long v = 1, long long p = 0) {
  h[v] = h[par[v] = p] + 1;
  for (auto u : g[v])
    if (u != p) pfs(u, v);
}
vector<long long> gmas(long long v, long long u) {
  vector<long long> v1 = {n + 1, v}, v2 = {u};
  while (v != u) {
    if (h[v] > h[u])
      v1.push_back(v = par[v]);
    else
      v2.push_back(u = par[u]);
  }
  v2.pop_back();
  while (!v2.empty()) v1.push_back(v2.back()), v2.pop_back();
  v1.push_back(n + 2);
  return v1;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  fac[0] = 1;
  for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * i % mod;
  cin >> n;
  for (int i = 1; i < n; i++) {
    long long v, u;
    cin >> v >> u;
    g[v].push_back(u), g[u].push_back(v);
  }
  pfs();
  for (int v = 1; v <= n; v++) {
    long long u;
    cin >> u;
    if (u) {
      if (v == u) return cout << 0, 0;
      vector<long long> vv = gmas(v, u);
      for (int i = 1; i + 1 < (long long)(vv.size()); i++) {
        long long v = vv[i - 1], u = vv[i], w = vv[i + 1];
        if (l[u].count(w) || r[u].count(v)) return cout << 0, 0;
        l[u][w] = v, r[u][v] = w;
      }
    }
  }
  long long ans = 1;
  for (int v = 1; v <= n; v++) {
    g[v].push_back(n + 1), g[v].push_back(n + 2);
    long long cnt = 0, po = 1;
    for (auto u : g[v]) {
      bool first = 1;
      while (u) {
        if (mk[u] > 0 && mk[u] < po) {
          first = 0;
          break;
        }
        if (mk[u] == po) return cout << 0, 0;
        mk[u] = po;
        u = r[v][u];
      }
      cnt += first, po++;
    }
    for (auto u : g[v]) mk[u] = 0;
    assert(cnt != 0);
    if (cnt != 1) {
      long long u = n + 1;
      while (r[v][u]) u = r[v][u];
      if (u == n + 2) return cout << 0, 0;
      cnt -= 2;
    }
    (ans *= fac[cnt]) %= mod;
  }
  cout << ans;
  return 0;
}
