Christmas was knocking on the door, and our protagonist, Bob, was preparing a spectacular present for his long-time second best friend Charlie. As chocolate boxes are lame, he decided to decorate a tree instead. Bob's tree can be represented as an undirected connected graph with n nodes (numbered 1 to n) and n-1 edges. Initially, Bob placed a decoration with label i on node i, for each 1 ≤ i ≤ n. However, as such a simple arrangement is lame, he decided to shuffle the decorations a bit. Formally, Bob did the following steps:

  * First, he listed the n-1 edges in some order.
  * Then, he considered the edges one by one in that order. For each edge (u, v), he swapped the decorations of node u with the one of node v.



After finishing, Bob seemed satisfied with the arrangement, so he went to sleep.

The next morning, Bob wakes up only to find out that his beautiful arrangement has been ruined! Last night, Bob's younger brother Bobo dropped some of the decorations on the floor while he was playing with the tree. Fortunately, no decorations were lost, so Bob can repair the tree in no time. However, he completely forgets how the tree looked like yesterday. Therefore, given the labels of the decorations still on the tree, Bob wants to know the number of possible configurations of the tree. As the result can be quite large, Bob will be happy if you can output the result modulo 1000000007 (10^9+7). Note that, it is possible that there exists no possible configurations.

Input

The first line contains a single integer n (2 ≤ n ≤ 500 000) — the number of nodes.

Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n), denoting that there is an edge connecting two nodes u and v. It is guaranteed that the given edges form a tree.

The last line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ n). For each i, a_i = 0 means that the decoration of node i has been dropped on the floor. Otherwise, a_i is the label of the decoration of node i. It is guaranteed that no label appears more than once.

Output

Output the number of possible configurations modulo 1000000007 (10^9+7).

Examples

Input


4
3 4
2 4
4 1
0 4 0 0


Output


2


Input


5
1 2
2 4
3 4
5 4
0 0 0 0 0


Output


12


Input


3
1 2
1 3
1 0 0


Output


0

Note

In the first example, the possible configurations of the tree are [2, 4, 1, 3] and [3, 4, 2, 1].

In the second example, note that while there are 4! = 24 possible permutations of the edges, each of them results in a possible configuration, there are only 12 different configurations.

In the third example, it is easy to see that the decoration 1 cannot stay at node 1 after the swaps.
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 5e5 + 7;
const int M = 1e9 + 7;
const int MEM = 1e7;
int c[MEM];
int ptr = 0;
struct Vector {
  int* v;
  int n = 0;
  Vector() { v = &c[ptr]; }
  int size() const { return n; };
  int& operator[](const int i) {
    assert(i < n);
    return v[i];
  }
  int& back() { return v[n - 1]; };
  void push_back(int val) {
    v[n] = val;
    ptr++;
    n++;
  }
  int* begin() { return &v[0]; }
  int* end() { return &v[n - 1] + 1; }
};
vector<int> g[N];
vector<int> eg[N], erg[N];
int mul(int a, int b) { return (a * (long long)b) % M; }
int add(int a, int b) {
  int c = a + b;
  if (c < 0) c += M;
  if (c >= M) c -= M;
  return c;
}
int pw(int a, int n) {
  int res = 1;
  while (n) {
    if (n % 2 == 0) {
      a = mul(a, a);
      n /= 2;
    } else {
      res = mul(res, a);
      n--;
    }
  }
  return res;
}
int inv(int x) { return pw(x, M - 2); }
int tin[N], tout[N], park[N];
bool par(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int tt = 0;
void dfs(int v, int pr) {
  tin[v] = tt++;
  for (int to : g[v]) {
    if (to != pr) {
      park[to] = v;
      dfs(to, v);
    }
  }
  tout[v] = tt++;
}
Vector gen_path(int a, int b) {
  Vector ret;
  while (!par(a, b)) {
    ret.push_back(a);
    a = park[a];
  }
  ret.push_back(a);
  vector<int> go;
  while (b != a) {
    go.push_back(b);
    b = park[b];
  }
  reverse(go.begin(), go.end());
  for (int v : go) {
    ret.push_back(v);
  }
  return ret;
}
int vis[N];
void zhfs(int v) {
  vis[v] = 1;
  for (int to : eg[v]) {
    if (!vis[to]) {
      zhfs(to);
    } else if (vis[to] == 1) {
      cout << 0 << '\n';
      exit(0);
    }
  }
  vis[v] = 2;
}
struct hsh {
  int operator()(const pair<int, int>& a) const {
    return a.first * (long long)1e9 + a.second;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  unordered_map<pair<int, int>, int, hsh> mp;
  unordered_map<pair<int, int>, int, hsh> nxt;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
    mp[{a, b}] = mp[{b, a}] = i;
  }
  dfs(0, -1);
  auto add_edge = [&](int s, int t) {
    eg[s].push_back(t);
    erg[t].push_back(s);
  };
  vector<int> arr(n);
  vector<int> first(n, -1);
  vector<int> last(n, -1);
  long long tot = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i]--;
    if (arr[i] != -1) {
      if (i == arr[i]) {
        cout << 0 << '\n';
        exit(0);
      }
      ptr = 0;
      auto b = gen_path(i, arr[i]);
      tot += b.size();
      if (tot > 3 * n) {
        cout << 0 << '\n';
        return 0;
      }
      Vector ids;
      for (int i = 1; i < (int)b.size(); i++) {
        ids.push_back(mp[{b[i - 1], b[i]}]);
      }
      for (int i = 1; i < (int)ids.size(); i++) {
        if (nxt.count({b[i], ids[i - 1]})) {
          cout << 0 << '\n';
          return 0;
        }
        nxt[{b[i], ids[i - 1]}] = ids[i];
      }
      first[i] = ids[0];
      last[arr[i]] = ids.back();
    }
    ptr = 0;
  }
  int ans = 1;
  for (int i = 0; i < n; i++) {
    ptr = 0;
    Vector ids;
    map<int, int> t;
    map<int, int> d;
    for (int j : g[i]) {
      ids.push_back(mp[{i, j}]);
    }
    set<int> q;
    for (int x : ids) {
      t[x] = 0;
      q.insert(x);
    }
    set<int> nq;
    map<int, int> mp;
    function<int(int)> get = [&](int v) {
      if (!mp.count(v)) {
        mp[v] = v;
      }
      if (mp[v] == v) {
        return v;
      } else {
        return mp[v] = get(mp[v]);
      }
    };
    for (int x : ids) {
      if (nxt.count({i, x}))
        if (t.count(nxt[{i, x}])) {
          d[nxt[{i, x}]] = x;
          t[nxt[{i, x}]]++;
          if (get(nxt[{i, x}]) == get(x)) {
            cout << 0 << '\n';
            return 0;
          }
          mp[get(nxt[{i, x}])] = get(x);
          q.erase(nxt[{i, x}]);
        }
    }
    for (auto& c : t) {
      if (c.second > 1) {
        cout << 0 << '\n';
        return 0;
      }
    }
    int need = q.size();
    if (first[i] != -1) {
      need--;
      if (t[first[i]] > 0) {
        cout << 0 << '\n';
        return 0;
      }
    }
    if (last[i] != -1) {
      need--;
      if (nxt.count({i, last[i]})) {
        cout << 0 << '\n';
        return 0;
      }
    }
    if (first[i] != -1 && last[i] != -1 && get(first[i]) == get(last[i]) &&
        q.size() > 1) {
      cout << 0 << '\n';
      return 0;
    }
    for (int i = 1; i <= need; i++) {
      ans = mul(ans, i);
    }
  }
  cout << ans << endl;
}
