#include <bits/stdc++.h>
using namespace std;
const int MaxN = 5e5 + 100;
const int LogN = 20;
const int Inf = 2e15;
double eps = 1e-3;
int md = 1e9 + 7;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int gcdex(int a, int mod = md) {
  int g = mod, r = a, x = 0, y = 1;
  while (r != 0) {
    int q = g / r;
    g %= r;
    swap(g, r);
    x -= q * y;
    swap(x, y);
  }
  return x < 0 ? x + mod : x;
}
vector<int> Gr[MaxN];
int Pr[MaxN];
int Ht[MaxN];
int S, T;
void dfs(int v, int p = -1) {
  if (p != -1) Ht[v] = Ht[p] + 1;
  Pr[v] = p;
  for (int to : Gr[v]) {
    if (p == to) continue;
    dfs(to, v);
  }
}
int Cnt = 0;
vector<pair<int, int> > Kt[MaxN];
int Ed[MaxN];
void findPath(int v, int u) {
  vector<int> At;
  vector<int> Bt;
  At.push_back(S);
  Bt.push_back(T);
  while (Ht[v] > Ht[u]) {
    At.push_back(v);
    v = Pr[v];
  }
  while (Ht[u] > Ht[v]) {
    Bt.push_back(u);
    u = Pr[u];
  }
  while (u != v) {
    At.push_back(v);
    Bt.push_back(u);
    v = Pr[v];
    u = Pr[u];
  }
  At.push_back(v);
  reverse(Bt.begin(), Bt.end());
  for (int a : Bt) At.push_back(a);
  for (int i = 1; i < (At).size() - 1; i++) {
    Kt[At[i]].push_back({At[i - 1], At[i + 1]});
  }
  Cnt += (At).size() - 3;
}
int Nxt[MaxN], Prv[MaxN];
bool used[MaxN];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int N;
  cin >> N;
  if (N == 1) {
    cout << 1 << '\n';
    return 0;
  }
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    Gr[a].push_back(b);
    Gr[b].push_back(a);
  }
  S = N;
  T = N + 1;
  for (int i = 0; i < N; i++) {
    Gr[i].push_back(S);
    Gr[i].push_back(T);
  }
  vector<int> Mt(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (i + 1 == a) {
      cout << 0 << '\n';
      return 0;
    }
    Mt[i] = a;
  }
  dfs(0);
  for (int i = 0; i < N; i++) {
    if (Mt[i] == 0) continue;
    findPath(i, Mt[i] - 1);
    if (Cnt > 2 * N - 2) {
      cout << 0 << '\n';
      return 0;
    }
  }
  fill(Nxt, Nxt + N + 2, -1);
  fill(Prv, Prv + N + 2, -1);
  int Ans = 1;
  for (int i = 0; i < N; i++) {
    for (pair<int, int> k : Kt[i]) {
      if (Nxt[k.first] != -1 && Nxt[k.first] != k.second) {
        cout << 0 << '\n';
        return 0;
      }
      if (Prv[k.second] != -1 && Prv[k.second] != k.first) {
        cout << 0 << '\n';
        return 0;
      }
      Nxt[k.first] = k.second;
      Prv[k.second] = k.first;
    }
    for (int to : Gr[i]) {
      if (used[to]) continue;
      used[to] = 1;
      int k = Nxt[to];
      while (k != -1) {
        if (to == k) {
          cout << 0;
          return 0;
        }
        if (used[k]) break;
        used[k] = 1;
        k = Nxt[k];
      }
    }
    if (Nxt[S]) {
      int v = S;
      int cnt = 1;
      while (v != -1) {
        if (v == T) break;
        cnt++;
        v = Nxt[v];
      }
      if (v == T && cnt < (Gr[i]).size()) {
        cout << 0;
        return 0;
      }
    }
    int t = 0;
    for (int to : Gr[i])
      if (Prv[to] == -1 && to < N) t++;
    if (Prv[T] != -1) t--;
    for (int k = 2; k <= t; k++) Ans = 1ll * Ans * k % md;
    for (int to : Gr[i]) {
      used[to] = 0;
      Nxt[to] = Prv[to] = -1;
    }
  }
  cout << Ans;
  return 0;
}
