#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
using ll = long long;
int n, cnt, totsz, rtsz, rt, P, sz[N], a[N], idx;
pair<ll, int> p[N], q[N];
ll xx, kk, kinv, pw[N], pinv[N], in0[N], out0[N], in1[N], out1[N];
vector<int> v[N];
ll qpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % P;
    a = a * a % P;
    b >>= 1;
  }
  return ret;
}
bool vis[N];
void getrt(int pos, int fa) {
  sz[pos] = 1;
  int tmp = 0;
  for (auto &i : v[pos])
    if (!vis[i] && i != fa)
      getrt(i, pos), sz[pos] += sz[i], tmp = max(tmp, sz[i]);
  tmp = max(tmp, totsz - sz[pos]);
  if (tmp < rtsz) rtsz = tmp, rt = pos;
}
void getp(int pos, int fa, int dep, ll s1, ll s2) {
  s1 = (s1 * kk + a[pos]) % P;
  s2 = (s2 + a[pos] * (!dep ? 0 : pw[dep - 1])) % P, ++dep;
  p[++idx] = {(xx - s1 + P) * pinv[dep] % P, pos};
  q[idx] = {s2, pos};
  for (auto &i : v[pos])
    if (!vis[i] && i != fa) getp(i, pos, dep, s1, s2);
}
void calc(int pos, int fl, int dep, ll s1, ll s2) {
  idx = 0;
  s1 = (s1 * kk + a[pos]) % P;
  s2 = (s2 + a[pos] * (!dep ? 0 : pw[dep - 1])) % P, ++dep;
  p[++idx] = {(xx - s1 + P) * pinv[dep] % P, pos};
  q[idx] = {s2, pos};
  for (auto &i : v[pos])
    if (!vis[i]) getp(i, pos, dep, s1, s2);
  sort(p + 1, p + idx + 1);
  sort(q + 1, q + idx + 1);
  for (int cnt = 0, i = 1, j = 1; i <= idx; i++) {
    while (j <= idx && q[j].first <= p[i].first) {
      if (j == 1 || q[j].first != q[j - 1].first) cnt = 0;
      ++cnt;
      ++j;
    }
    if (j != 1 && q[j - 1].first == p[i].first) out1[p[i].second] += cnt * fl;
  }
  for (int cnt = 0, i = 1, j = 1; i <= idx; i++) {
    while (j <= idx && p[j].first <= q[i].first) {
      if (j == 1 || p[j].first != p[j - 1].first) cnt = 0;
      ++cnt;
      ++j;
    }
    if (j != 1 && p[j - 1].first == q[i].first) in1[q[i].second] += cnt * fl;
  }
}
void solve(int pos) {
  vis[pos] = 1;
  calc(pos, 1, 0, 0, 0);
  for (auto &i : v[pos])
    if (!vis[i]) {
      calc(i, -1, 1, a[pos], 0);
      totsz = sz[i];
      rtsz = INT_MAX;
      getrt(i, pos);
      solve(rt);
    }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> P >> kk >> xx;
  kinv = qpow(kk, P - 2);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = pw[0] = pinv[0] = 1; i <= n; i++)
    pw[i] = pw[i - 1] * kk % P, pinv[i] = pinv[i - 1] * kinv % P;
  int t1, t2;
  for (int i = 1; i < n; i++)
    cin >> t1 >> t2, v[t1].push_back(t2), v[t2].push_back(t1);
  rtsz = INT_MAX;
  totsz = n;
  getrt(1, 0);
  solve(rt);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    in0[i] = n - in1[i];
    out0[i] = n - out1[i];
    ans += 2 * in1[i] * in0[i] + 2 * out1[i] * out0[i] + in0[i] * out1[i] +
           in1[i] * out0[i];
  }
  cout << ll(n) * n * n - ans / 2 << endl;
  return 0;
}
