This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}
const int N = 73;
const ll mod = 1e9 + 7;
ll qpow(ll a, ll b){
    ll res = 1;
    for(; b > 0; b >>= 1, a = a * a % mod)
        if(b & 1)
            res = res * a % mod;
    return res;
}
int n, m, k;
vector<int> g[N], gm[N];
vector<int> path[N][N];
bool in[20][N];
bool vis[N];
int x[20], y[20];
ll res;
struct DSU{
    vector<ll> p, sz;
    int comp;
    void init(int n){
        p.resize(n + 2, 0);
        sz.resize(n + 2, 0);
        comp = n;
        for(int i = 1; i <= n; ++i)
            p[i] = i, sz[i] = 1;
    }
    int find_set(int u){
        return u == p[u] ? u : p[u] = find_set(p[u]);
    }
    bool join_sets(int u, int v){
        u = find_set(u), v = find_set(v);
        if(u == v)
            return 0;
        --comp;
        if(sz[u] < sz[v])
            swap(u, v);
        p[v] = u;
        sz[u] += sz[v];
        return 1;
    }
};
void get_path(int src){
    queue<int> q;
    path[src][src].push_back(src);
    q.push(src);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int v : g[u]){
            if(path[src][v].empty()){
                path[src][v] = path[src][u];
                path[src][v].push_back(v);
                q.push(v);
            }
        }
    }
}
void solve(){
    cin >> n >> m >> k;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 0; i < m; ++i)
        cin >> x[i] >> y[i];
    for(int i = 1; i <= n; ++i)
        get_path(i);
    for(int i = 0; i < m; ++i){
        if(sz(path[x[i]][y[i]]) <= 2){
            cout << 0;
            return;
        }
    }
    for(int i = 0; i < m; ++i)
        for(int x : path[x[i]][y[i]])
            in[i][x] = 1;
    for(int i = 0; i < m; ++i){
        for(int j = i + 1; j < m; ++j){
            int cnt = 0;
            for(int x = 1; x <= n; ++x){
                if(in[i][x] and in[j][x])
                    ++cnt;
            }
            if(cnt > 1){
                gm[i].push_back(j);
                gm[j].push_back(i);
            }
        }
    }
    for(int mask = 0; mask < (1 << m); ++mask){
        DSU dsu;
        dsu.init(m);
        for(int i = 0; i < m; ++i){
            if(mask & (1 << i)){
                for(int v : gm[i])
                    if(mask & (1 << v))
                        dsu.join_sets(i, v);
            }
        }
        set<pii> st;
        for(int i = 0; i < m; ++i){
            if(mask & (1 << i)){
                for(int j = 0; j < sz(path[x[i]][y[i]]) - 1; ++j){
                    int u = path[x[i]][y[i]][j], v = path[x[i]][y[i]][j + 1];
                    if(u > v)
                        swap(u, v);
                    st.insert({u, v});
                }
            }
        }
        // debug(st);
        int rem = n - 1 - sz(st);
        ll cur = qpow(k, rem);
        for(int i = 0; i < m; ++i){
            if(mask & (1 << i) and dsu.find_set(i) == i)
                cur = cur * k % mod;
        }
        // debug(bitset<5>(mask), cur);
        if(__builtin_popcount(mask) & 1)
            res += mod - cur;
        else
            res += cur;
        while(res >= mod)
            res -= mod;
    }
    cout << res;
}
int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "coloring"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:180:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |