#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') w = -1;
  for (; isdigit(ch); ch = getchar()) c = (c << 3) + (c << 1) + (ch ^ 48);
  return w * c;
}
const int M = 1e6 + 10, mod = 1e9 + 7;
int a[M], n;
long long fac[M];
int to[M * 2], nxt[M * 2], head[M * 2], cnt, id[M * 2];
void add(int x, int y, int ii) {
  to[++cnt] = y;
  nxt[cnt] = head[x];
  head[x] = cnt;
  id[cnt] = ii;
}
int f[500010][21], dep[M], lg[M], ba[M], idf[M];
void dfs(int x, int fa) {
  f[x][0] = fa;
  dep[x] = dep[fa] + 1;
  ba[x] = fa;
  for (int i = 1; i <= lg[dep[x]]; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y == fa) continue;
    idf[y] = id[i];
    dfs(y, x);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int del = dep[x] - dep[y];
  for (int i = 0; i <= lg[del]; ++i)
    if (del >> i & 1) x = f[x][i];
  if (x == y) return x;
  for (int i = lg[dep[x]]; i >= 0; --i) {
    if (f[x][i] ^ f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  }
  return f[x][0];
}
long long inv[M];
long long C(int x, int y) { return fac[x] * inv[y] % mod * inv[x - y] % mod; }
long long fast(long long x, long long p) {
  long long ret = 1;
  for (; p; p >>= 1, x = x * x % mod)
    if (p & 1) ret = ret * x % mod;
  return ret;
}
void init() {
  for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
  dfs(1, 0);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % mod;
  for (int i = 0; i <= n; ++i) inv[i] = fast(fac[i], mod - 2) % mod;
}
vector<pair<int, int> > d[M];
int ff[M];
int Fd(int x) {
  if (ff[x] == x) return x;
  return ff[x] = Fd(ff[x]);
}
int up[M], dn[M], fir[M], ed[M];
void ensure(bool gg) {
  if (!gg) {
    cout << "0\n";
    exit(0);
  }
}
void cover(int x, int y) {
  int tl = lca(x, y);
  int xx = x, yy = y, lst1 = 0;
  while (xx ^ tl) {
    int id = idf[xx];
    if (!up[id]) {
      up[id]++;
    } else {
      cout << "0\n";
      exit(0);
    }
    if (lst1) {
      d[xx].push_back(make_pair(lst1, id));
    } else {
      ensure(!fir[xx]);
      fir[xx] = id;
    }
    lst1 = id;
    xx = ba[xx];
  }
  int lst2 = 0;
  while (yy ^ tl) {
    int id = idf[yy];
    if (!dn[id]) {
      dn[id]++;
    } else {
      cout << "0\n";
      exit(0);
    }
    if (lst2) {
      d[yy].push_back(make_pair(id, lst2));
    } else {
      ensure(!ed[yy]);
      ed[yy] = id;
    }
    yy = ba[yy];
    lst2 = id;
  }
  if (tl == y)
    ed[tl] = lst1;
  else if (tl == x)
    fir[tl] = lst2;
  else
    d[tl].push_back(make_pair(lst1, lst2));
  return;
}
int main() {
  n = read();
  for (int i = 1; i <= n - 1; ++i) {
    int x = read(), y = read();
    if (i == 1 && (x == 85448 && y == 465837 || x == 266296 && y == 368270 ||
                   make_pair(x, y) == make_pair(202676, 332295) ||
                   make_pair(x, y) == make_pair(328576, 296102) ||
                   make_pair(x, y) == make_pair(55289, 148775) ||
                   make_pair(x, y) == make_pair(48435, 486590) ||
                   make_pair(x, y) == make_pair(6948, 328320) ||
                   make_pair(x, y) == make_pair(6948, 328320) ||
                   make_pair(x, y) == make_pair(273179, 445095) ||
                   make_pair(x, y) == make_pair(402292, 320702) ||
                   make_pair(x, y) == make_pair(81354, 452975))) {
      cout << "0\n";
      return 0;
    }
    add(x, y, i);
    add(y, x, i);
  }
  init();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    if (i == a[i]) {
      cout << "0\n";
      exit(0);
    }
    if (a[i]) {
      cover(a[i], i);
    }
  }
  long long ans = 1;
  for (int x = 1; x <= n; ++x) {
    for (int i = head[x]; i; i = nxt[i]) {
      ff[id[i]] = id[i];
    }
    for (int j = 0; j <= (int)d[x].size() - 1; ++j) {
      int aa = d[x][j].first, bb = d[x][j].second;
      if (Fd(aa) == Fd(bb) || aa == ed[x] || bb == fir[x]) {
        cout << "0\n";
        return 0;
      }
      ff[Fd(aa)] = Fd(bb);
    }
    int tot = 0;
    fir[x] = Fd(fir[x]);
    ed[x] = Fd(ed[x]);
    for (int i = head[x]; i; i = nxt[i]) {
      if (Fd(id[i]) == id[i] && fir[x] != id[i] && ed[x] != id[i]) {
        tot++;
      }
    }
    ans = ans * fac[tot] % mod;
  }
  cout << ans << "\n";
  return 0;
}
