#include <bits/stdc++.h>   using namespace std;   void Freopen() {     freopen(          title           .in ,          r , stdin);     freopen(          title           .out ,          w , stdout);   }   int read() {     int g = 0, f = 1;     char ch = getchar();     while (ch <  0  ||  9  < ch) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while ( 0  <= ch && ch <=  9 ) {       g = g * 10 + ch -  0 ;       ch = getchar();     }     return g * f;   }   const int N = 5e5 + 5;   const int mod = 1e9 + 7;   int st[N][20], dep[N], vis[N], ta, tb, top, sa[N], sb[N], sta[N];   vector<int> G[N], F[N], R[N], C[N], size[N];   int n, fir[N], las[N], fac[N], du[N];   unordered_map<int, int> M[N];   void dfs(int x, int fa) {     st[x][0] = fa;     dep[x] = dep[fa] + 1;     for (int i = (1); i <= (19); i++)       if (st[x][i - 1])         st[x][i] = st[st[x][i - 1]][i - 1];       else         break;     for (auto y : G[x])       if (y ^ fa) dfs(y, x);   }   int Lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     for (int i = (19); i >= (0); i--)       if (dep[st[x][i]] >= dep[y]) x = st[x][i];     if (x == y) return x;     for (int i = (19); i >= (0); i--)       if (st[x][i] != st[y][i]) x = st[x][i], y = st[y][i];     return st[x][0];   }   int get(int x, vector<int>& f) { return f[x] == x ? x : f[x] = get(f[x], f); }   bool Merge(int x, int y, vector<int>& f, vector<int>& size, vector<int>& ru,              vector<int>& cu) {     cu[x]++, ru[y]++;     if (cu[x] > 1 || ru[y] > 1) return false;     int fx = get(x, f), fy = get(y, f);     if (fx == fy) return false;     size[fx] += size[fy];     f[fy] = fx;     return true;   }   signed main() {     n = read();     memset(fir, -1, sizeof(fir));     memset(las, -1, sizeof(las));     if (n == 1) return cout << 1, signed();     fac[0] = 1;     for (int i = (1); i <= (n); i++) fac[i] = 1ll * fac[i - 1] * i % mod;     for (int i = (1); i <= (n - 1); i++) {       int x = read(), y = read();       G[x].push_back(y), G[y].push_back(x);       M[x][y] = G[x].size() - 1, M[y][x] = G[y].size() - 1;       F[x].push_back(G[x].size() - 1), F[y].push_back(G[y].size() - 1);       size[x].push_back(1), size[y].push_back(1);       R[x].push_back(0), R[y].push_back(0);       C[x].push_back(0), C[y].push_back(0);       du[x]++, du[y]++;     }     dfs(1, 0);     for (int x = (1); x <= (n); x++) {       int y = read();       if (y == 0) continue;       int anc = Lca(x, y);       if (x == y) return cout << 0, signed();       if (vis[y]) return cout << 0, signed();       vis[y] = 1;       ta = tb = top = 0;       for (int t = x; t != anc; t = st[t][0]) sa[++ta] = t;       for (int t = y; t != anc; t = st[t][0]) sb[++tb] = t;       for (int i = (1); i <= (tb); i++) sta[++top] = sb[i];       sta[++top] = anc;       for (int i = (ta); i >= (1); i--) sta[++top] = sa[i];       for (int i = (2); i <= (top - 1); i++) {         int ipre = M[sta[i]][sta[i - 1]], isuf = M[sta[i]][sta[i + 1]];         if (!Merge(ipre, isuf, F[sta[i]], size[sta[i]], R[sta[i]], C[sta[i]]))           return cout << 0, signed();       }       fir[sta[1]] = M[sta[1]][sta[2]];       las[sta[top]] = M[sta[top]][sta[top - 1]];       for (int i = (1); i <= (top); i++) {         if (fir[sta[i]] >= 0 && las[sta[i]] >= 0 &&             get(fir[sta[i]], F[sta[i]]) == get(las[sta[i]], F[sta[i]]) &&             size[sta[i]][get(fir[sta[i]], F[sta[i]])] != du[sta[i]])           return cout << 0, signed();       }     }     int ans = 1;     for (int i = (1); i <= (n); i++) {       for (int j = (0); j <= (du[i] - 1); j++) vis[j] = 0;       int cnt = 0;       for (int j = (0); j <= (du[i] - 1); j++) {         int x = get(j, F[i]);         if (vis[x]) continue;         vis[x] = 1;         cnt++;       }       if (fir[i] >= 0) cnt--;       if (las[i] >= 0) cnt--;       if (fir[i] >= 0 && las[i] >= 0 && get(fir[i], F[i]) == get(las[i], F[i]))         cnt++;       ans = 1ll * ans * fac[cnt] % mod;     }     return cout << ans, signed();   }