#include <bits/stdc++.h>
using namespace std;
const int mxn = 5e5 + 5;
const int mod = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
int pw(int x, int y) {
  int r = 1;
  while (y) {
    if (y & 1) r = 1LL * r * x % mod;
    x = 1LL * x * x % mod;
    y >>= 1;
  }
  return r;
}
int n, up[mxn][30], h[mxn], l = 29, a[mxn], par[mxn], cnt = 0, nx[mxn], pr[mxn],
                            vis[mxn];
vector<int> g[mxn], vin, vout;
vector<pair<int, int> > con[mxn];
bool ok = true;
void dfs(int v, int p) {
  h[v] = h[p] + 1;
  up[v][0] = p;
  for (long long i = 1; i <= l; i++) {
    up[v][i] = up[up[v][i - 1]][i - 1];
  }
  for (long long u = 0; u < g[v].size(); u++) {
    if (g[v][u] != p) {
      par[g[v][u]] = v;
      dfs(g[v][u], v);
    }
  }
}
int get_par(int v, int he) {
  for (int i = 0; i < l; i++) {
    if (he >> i & 1) v = up[v][i];
  }
  return v;
}
int lca(int v, int u) {
  if (h[u] > h[v]) swap(u, v);
  v = get_par(v, h[v] - h[u]);
  if (u == v) return v;
  for (int i = l; i >= 0; i--) {
    if (up[v][i] != up[u][i]) {
      u = up[u][i];
      v = up[v][i];
    }
  }
  return up[u][0];
}
void f(int u, int v) {
  int r = lca(u, v);
  vin.clear();
  vout.clear();
  vout.push_back(n + 1);
  vin.push_back(n + 2);
  if (u != r) {
    vout.push_back(u);
    u = par[u];
    while (u != r) {
      vout.push_back(u);
      u = par[u];
    }
  }
  vout.push_back(u);
  if (v != r) {
    vin.push_back(v);
    v = par[v];
    while (v != r) {
      vin.push_back(v);
      v = par[v];
    }
  }
  vout.insert(vout.end(), vin.rbegin(), vin.rend());
  for (int i = 1; i + 1 < vout.size(); i++) {
    con[vout[i]].push_back({vout[i - 1], vout[i + 1]});
  }
  cnt += vout.size() - 3;
  if (cnt > 2 * n - 2) {
    ok = false;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    g[i].push_back(n + 1);
    g[i].push_back(n + 2);
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n + 2; i++) {
    nx[i] = 0;
    pr[i] = 0;
    vis[i] = 0;
  }
  if (n == 1) {
    cout << 1 << '\n';
    ;
    return 0;
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) continue;
    ok = true;
    f(i, a[i]);
    if (!ok) {
      cout << 0 << '\n';
      ;
      return 0;
    }
  }
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    for (auto e : con[i]) {
      int u = e.first, v = e.second;
      if (nx[u] && nx[u] != v) {
        cout << 0 << '\n';
        ;
        return 0;
      }
      if (pr[v] && pr[v] != u) {
        cout << 0 << '\n';
        ;
      }
      nx[u] = v;
      pr[v] = u;
    }
    for (auto u : g[i]) {
      if (vis[u]) continue;
      int p = nx[u];
      while (p) {
        if (u == p) {
          cout << 0 << '\n';
          ;
          return 0;
        }
        if (vis[p]) break;
        vis[p] = 1;
        p = nx[p];
      }
    }
    if (nx[n + 1]) {
      long long p = n + 1;
      long long sz = 1;
      while (p) {
        if (p == n + 2) break;
        sz++;
        p = nx[p];
      }
      if (p == n + 2 && sz < g[i].size()) {
        cout << 0 << '\n';
        ;
        return 0;
      }
    }
    cnt = 0;
    for (auto u : g[i]) {
      if (u <= n && !pr[u]) cnt++;
    }
    if (pr[n + 2]) cnt--;
    for (int j = 1; j <= cnt; j++) ans = 1LL * ans * j % mod;
    for (auto u : g[i]) pr[u] = 0, nx[u] = 0, vis[u] = 0;
  }
  cout << ans << '\n';
  ;
  return 0;
}
