#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
void FAIL() {
  printf("0");
  exit(0);
}
const int N = 5e5 + 10, mod = 1e9 + 7;
int In[N], Out[N], To[N], vis[N], n;
int sol(vector<int> &G, vector<pair<int, int> > &E) {
  G.push_back(0), G.push_back(n + 1);
  for (int i = 0; i < E.size(); ++i)
    Out[E[i].first]++, In[E[i].second]++, To[E[i].first] = E[i].second;
  for (int i = 0; i < G.size(); ++i)
    if (In[G[i]] > 1 || Out[G[i]] > 1) FAIL();
  for (int i = 0; i < G.size(); ++i)
    if (!vis[G[i]]) {
      int cur = G[i];
      vis[cur] = 1;
      while (~To[cur] && !vis[To[cur]]) cur = To[cur], vis[cur] = 1;
      if (To[cur] == G[i]) FAIL();
    }
  int cur = 0;
  while (~To[cur]) cur = To[cur];
  if (cur == n + 1 && (int)G.size() - (int)E.size() > 1) FAIL();
  for (int i = 0; i < G.size(); ++i)
    In[G[i]] = Out[G[i]] = vis[G[i]] = 0, To[G[i]] = -1;
  return max((int)G.size() - (int)E.size() - 2, 0);
}
vector<int> G[N];
vector<pair<int, int> > E[N];
int fa[N], dep[N];
void dfs1(int u, int last) {
  fa[u] = last, dep[u] = dep[last] + 1;
  for (int i = 0; i < G[u].size(); ++i) {
    int v = G[u][i];
    if (v == last) continue;
    dfs1(v, u);
  }
}
vector<int> P, Q;
void getpath(int x, int y) {
  P.clear(), Q.clear();
  int flg = 0;
  if (dep[x] < dep[y]) swap(x, y), flg = 1;
  P.push_back(x);
  while (dep[x] > dep[y]) x = fa[x], P.push_back(x);
  if (x == y) {
    if (flg) reverse(P.begin(), P.end());
    return;
  }
  Q.push_back(y);
  while (fa[x] != fa[y]) x = fa[x], y = fa[y], P.push_back(x), Q.push_back(y);
  P.push_back(fa[x]);
  for (int i = (int)Q.size() - 1; i >= 0; --i) P.push_back(Q[i]);
  if (flg) reverse(P.begin(), P.end());
}
int tot;
int a[N];
int fac[N];
int main() {
  rd(n);
  for (int i = 1, x, y; i < n; ++i)
    rd(x), rd(y), G[x].push_back(y), G[y].push_back(x);
  dfs1(1, 0);
  for (int i = 1; i <= n; ++i) rd(a[i]);
  for (int i = 1; i <= n; ++i)
    if (a[i]) {
      if (a[i] == i) FAIL();
      getpath(a[i], i);
      if ((tot += (int)P.size() - 1) > 2 * n - 2) FAIL();
      int m = (int)P.size() - 1;
      E[P[0]].push_back(make_pair(0, P[1]));
      E[P[m]].push_back(make_pair(P[m - 1], n + 1));
      for (int i = 1; i < m; ++i)
        E[P[i]].push_back(make_pair(P[i - 1], P[i + 1]));
    }
  memset(To, -1, sizeof(To));
  int ans = 1;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * (long long)i % mod;
  for (int i = 1; i <= n; ++i)
    ans = ans * (long long)fac[sol(G[i], E[i])] % mod;
  printf("%d", ans);
  return 0;
}
