#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const long long &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
int MOD = 1e9 + 7;
void ADD(long long &x, long long v) {
  x = (x + v) % MOD;
  if (x < 0) x += MOD;
}
const int SIZE = 1e6 + 10;
void fail() {
  W(0);
  exit(0);
}
long long fac[SIZE];
vector<int> e[SIZE];
int jump[19 + 1][SIZE], lv[SIZE];
void __init(int x, int lt) {
  jump[0][x] = lt;
  for (int i = 0; i < (((int)(e[x]).size())); ++i) {
    int y = e[x][i];
    if (y != lt) {
      lv[y] = lv[x] + 1;
      __init(y, x);
    }
  }
}
int adv(int x, int v) {
  for (int i = 0; (1 << i) <= v; i++) {
    if ((v >> i) & 1) x = jump[i][x];
  }
  return x;
}
int lca(int x, int y) {
  if (lv[x] > lv[y])
    x = adv(x, lv[x] - lv[y]);
  else
    y = adv(y, lv[y] - lv[x]);
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (jump[i][x] != jump[i][y]) {
      x = jump[i][x];
      y = jump[i][y];
    }
  return jump[0][x];
}
int dis(int x, int y) {
  int z = lca(x, y);
  return lv[x] + lv[y] - lv[z] * 2;
}
void build(int root, int N) {
  __init(root, root);
  for (int i = (1); i < (19 + 1); ++i) {
    for (int x = 0; x < (N); ++x) jump[i][x] = jump[i - 1][jump[i - 1][x]];
  }
}
vector<pair<int, int> > ord[SIZE];
int st[SIZE], ed[SIZE];
struct Union_Find {
  int d[SIZE], num[SIZE];
  void init(int n) {
    for (int i = 0; i < (n); ++i) d[i] = i, num[i] = 1;
  }
  int find(int x) { return (x != d[x]) ? (d[x] = find(d[x])) : x; }
  bool is_root(int x) { return d[x] == x; }
  bool uu(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return 0;
    if (num[x] > num[y]) swap(x, y);
    num[y] += num[x];
    d[x] = y;
    return 1;
  }
} U;
bool check(vector<pair<int, int> > &gg) {
  vector<int> tmp;
  vector<int> d;
  for (int i = 0; i < (((int)(gg).size())); ++i) {
    tmp.push_back(gg[i].second);
    d.push_back(gg[i].second);
    d.push_back(gg[i].first);
    if (i && gg[i].first == gg[i - 1].first) fail();
  }
  (sort(tmp.begin(), tmp.end()),
   tmp.resize(distance(tmp.begin(), unique(tmp.begin(), tmp.end()))));
  if (((int)(tmp).size()) != ((int)(gg).size())) fail();
  (sort(d.begin(), d.end()),
   d.resize(distance(d.begin(), unique(d.begin(), d.end()))));
  U.init(((int)(d).size()));
  for (int i = 0; i < (((int)(gg).size())); ++i) {
    int x = (lower_bound(d.begin(), d.end(), gg[i].first) - d.begin());
    int y = (lower_bound(d.begin(), d.end(), gg[i].second) - d.begin());
    if (!U.uu(x, y)) {
      fail();
    }
  }
  return ((int)(d).size()) && d[0] == -1 && d.back() == MOD &&
         U.find(0) == U.find(((int)(d).size()) - 1);
}
long long solve(int x) {
  if (st[x] != -1) ord[x].push_back(make_pair(-1, st[x]));
  if (ed[x] != -1) ord[x].push_back(make_pair(ed[x], MOD));
  if (st[x] == ed[x] && st[x] != -1 && ((int)(e[x]).size()) > 1) fail();
  (sort(ord[x].begin(), ord[x].end()),
   ord[x].resize(
       distance(ord[x].begin(), unique(ord[x].begin(), ord[x].end()))));
  if (check(ord[x])) {
    if (((int)(e[x]).size()) + 1 != ((int)(ord[x]).size())) fail();
  }
  return fac[max(((int)(e[x]).size()) - ((int)(ord[x]).size()), 0)];
}
void assign(int &x, int v) {
  if (x == v || x == -1) {
    x = v;
  } else {
    fail();
  }
}
int dp[2][SIZE];
void dfs(int x, int lt) {
  for (int y : e[x]) {
    if (y == lt) continue;
    dfs(y, x);
    for (int j = 0; j < (2); ++j) dp[j][x] += dp[j][y];
  }
  if (dp[0][x] && dp[1][x]) {
    fail();
  }
  if (dp[0][x]) {
    ord[lt].push_back(make_pair(x, jump[0][lt]));
  }
  if (dp[1][x]) {
    ord[lt].push_back(make_pair(jump[0][lt], x));
  }
}
int main() {
  fac[0] = 1;
  for (int i = (1); i < (SIZE); ++i) {
    fac[i] = fac[i - 1] * i % MOD;
  }
  memset((st), -1, sizeof((st)));
  memset((ed), -1, sizeof((ed)));
  int n;
  R(n);
  for (int i = (1); i < (n); ++i) {
    int x, y;
    R(x, y);
    x--;
    y--;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  build(0, n);
  for (int y = 0; y < (n); ++y) {
    int x;
    R(x);
    if (x) {
      x--;
      if (x == y) fail();
      int z = lca(x, y);
      if (z != x)
        assign(st[x], jump[0][x]);
      else
        assign(st[x], adv(y, lv[y] - lv[x] - 1));
      if (z != y)
        assign(ed[y], jump[0][y]);
      else
        assign(ed[y], adv(x, lv[x] - lv[y] - 1));
      if (z != x && z != y) {
        ord[z].push_back(
            make_pair(adv(x, lv[x] - lv[z] - 1), adv(y, lv[y] - lv[z] - 1)));
      }
      if (lv[x] > lv[z] + 1) {
        int w = adv(x, lv[x] - lv[z] - 1);
        dp[0][x]++;
        dp[0][w]--;
      }
      if (lv[y] > lv[z] + 1) {
        int w = adv(y, lv[y] - lv[z] - 1);
        dp[1][y]++;
        dp[1][w]--;
      }
    }
  }
  dfs(0, 0);
  long long an = 1;
  for (int i = 0; i < (n); ++i) {
    an = an * solve(i) % MOD;
  }
  W(an);
  return 0;
}
