#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 333;
const long long linf = 1e18 + 333;
const int N = 1e5 + 5;
int n, mod, k, x;
int power[N], inv_power[N], in[N], out[N], a[N];
vector<int> v[N];
inline int mul(int x, int y) { return (long long)x * y % mod; }
inline int add(int x, int y) { return x + y - (x + y >= mod ? mod : 0); }
int cpow(int x, int k) {
  int res = 1;
  while (k) {
    if (k & 1) res = mul(res, x);
    x = mul(x, x);
    k >>= 1;
  }
  return res;
}
int ch[N], up[N], down[N], up_exc[N], down_exc[N], len[N];
bool h[N];
void calc(int x, int p = -1) {
  ch[x] = 1;
  for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {
    int u = *it;
    if (u != p and !h[u]) {
      calc(u, x);
      ch[x] += ch[u];
    }
  }
}
int find(int x, int t, int p = -1) {
  for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {
    int u = *it;
    if (u != p and !h[u] and ch[u] * 2 >= t) {
      return find(u, t, x);
    }
  }
  return x;
}
int cent;
void dfs2(int x, int p, int d) {
  if (up[x] == ::x) {
    out[x]++;
    in[cent]++;
  }
  if (down[x] == ::x) {
    in[x]++;
    out[cent]++;
  }
  for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {
    int u = *it;
    if (u != p and !h[u]) {
      up[u] = add(mul(up[x], k), a[u]);
      up_exc[u] = add(mul(up_exc[x], k), a[u]);
      down[u] = add(down[x], mul(a[u], power[d + 2]));
      down_exc[u] = add(down_exc[x], mul(a[u], power[d + 1]));
      len[u] = d + 3;
      dfs2(u, x, d + 1);
    }
  }
}
int sz;
int vec[N];
map<int, int> M_in, M_out;
void dfs3(int p, int x) {
  vec[sz++] = x;
  for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {
    int u = *it;
    if (u != p and !h[u]) dfs3(x, u);
  }
}
void calc_in(int u) { in[u] += M_in[down_exc[u]]; };
void calc_out(int u) {
  int v = add(x, mod - up[u]);
  v = mul(v, inv_power[len[u]]);
  out[u] += M_out[v];
};
void make_in(int u) {
  int v = add(x, mod - up[u]);
  v = mul(v, inv_power[len[u]]);
  M_in[v]++;
};
void make_out(int u) { M_out[down_exc[u]]++; };
void f(int p) {
  M_in.clear();
  M_out.clear();
  for (__typeof((v[p]).begin()) it = (v[p]).begin(); it != (v[p]).end(); it++) {
    int u = *it;
    if (!h[u]) {
      sz = 0;
      dfs3(-1, u);
      for (int i = 0; i < sz; i++) {
        int x = vec[i];
        calc_in(x);
        calc_out(x);
      }
      for (int i = 0; i < sz; i++) {
        int x = vec[i];
        make_in(x);
        make_out(x);
      }
    }
  }
}
void dfs(int x = 1) {
  calc(x);
  int p = find(x, ch[x]);
  cent = p;
  h[p] = 1;
  up[p] = down[p] = a[p];
  len[p] = 1;
  for (__typeof((v[p]).begin()) it = (v[p]).begin(); it != (v[p]).end(); it++) {
    int u = *it;
    if (!h[u]) {
      up[u] = add(mul(up[p], k), a[u]);
      down[u] = add(down[p], mul(a[u], power[1]));
      up_exc[u] = down_exc[u] = a[u];
      len[u] = 2;
      dfs2(u, -1, 0);
    }
  }
  f(p);
  reverse(v[p].begin(), v[p].end());
  f(p);
  for (__typeof((v[p]).begin()) it = (v[p]).begin(); it != (v[p]).end(); it++) {
    if (!h[*it]) {
      dfs(*it);
    }
  }
}
int main() {
  scanf("%d %d %d %d", &n, &mod, &k, &x);
  power[0] = 1;
  power[1] = k;
  inv_power[0] = 1;
  inv_power[1] = cpow(k, mod - 2);
  for (int i = 2; i <= n; i++) {
    power[i] = mul(power[i - 1], power[1]);
    inv_power[i] = mul(inv_power[i - 1], inv_power[1]);
  }
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs();
  for (int i = 1; i <= n; i++) {
    if (a[i] == x) {
      in[i]++;
      out[i]++;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int in1 = in[i], out1 = out[i];
    int in0 = n - in1, out0 = n - out1;
    ans += (long long)2 * out0 * out1 + (long long)2 * in0 * in1 +
           (long long)in0 * out1 + (long long)in1 * out0;
  }
  ans = (long long)n * n * n - ans / 2;
  printf("%lld\n", ans);
  return 0;
}
