1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  unsigned x = Get<unsigned>();
  if (neg) x = -x;
  return static_cast<int>(x);
}

template<> ULL Input::Get<ULL>() {
  SkipWS();
  ULL x = 0;
  while(isdigit(UPeek())) {
    x = 10ULL * x + (Get<char>()-'0');
  }
  return x;
}

template<> LL Input::Get<LL>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  ULL x = Get<ULL>();
  if (neg) x = -x;
  return static_cast<LL>(x);
}

template<> string Input::Get<string>() {
  SkipWS();
  string s;
  while(!Eof() && !isspace(UPeek())) s += Get<char>();
  return s;
}

Input IN;

template<unsigned MOD>
class Modulo {
  unsigned v;
 public:
  Modulo(unsigned x=0):v(x) {}
  unsigned get() const { return v; }
  Modulo operator+(Modulo b) const {
    unsigned res = v+b.v;
    if (res >= MOD) res -= MOD;
    return res;
  }
  void operator+=(Modulo b) { *this = *this + b; }
  Modulo operator-(Modulo b) const { return *this + Modulo(MOD-b.v); }
  void operator-=(Modulo b) { *this = *this - b; }
  Modulo operator*(Modulo b) const { return Modulo(ULL(v) * ULL(b.v) % MOD); }
  void operator*=(Modulo b) { *this = *this * b; }
};

using Mod = Modulo<1000000007>;

struct Edge {
  int from,to;
};

inline bool operator<(const Edge &a, const Edge &b) {
  return a.from < b.from;
}

int num_vertices;
int num_lake;
int num_ocean;

vector<Edge> edges;
vector<int> edges_start;
vector<Edge> rev_edges;
vector<int> rev_edges_start;

void read_input() {
  int num_edges;
  IN(num_vertices, num_edges, num_lake, num_ocean);
  edges.reserve(2 * num_edges);
  REP(i, num_edges) {
    Edge edge;
    string arrow;
    IN(edge.from, arrow, edge.to);
    --edge.from;
    --edge.to;
    edges.push_back(edge);
    if (arrow == "--") {
      swap(edge.from, edge.to);
      edges.push_back(edge);
    } else {
      assert(arrow == "->");
    }
  }

  rev_edges = edges;
  for (Edge &edge : rev_edges) swap(edge.from, edge.to);

  {
    sort(edges.begin(), edges.end());
    edges_start.resize(num_vertices+1);
    int p = 0;
    edges_start[0]=p;
    REP(v, num_vertices) {
      while (p<int(edges.size()) && edges[p].from == v) ++p;
      edges_start[v+1] = p;
    }
  }

  {
    sort(rev_edges.begin(), rev_edges.end());
    rev_edges_start.resize(num_vertices+1);
    int p = 0;
    rev_edges_start[0]=p;
    REP(v, num_vertices) {
      while (p<int(rev_edges.size()) && rev_edges[p].from == v) ++p;
      rev_edges_start[v+1] = p;
    }
  }

}

// reverse topologically sorted
int num_components;
vector<vector<int>> component_members;
vector<int> in_component;

struct SccInfo {
  int index = -1;
  int lowlink = -1;
  bool on_stack = false;
};

struct DfsEntry {
  int v;
  int next_edge;
};

void calc_components() {
  in_component.assign(num_vertices, -1);
  num_components = 0;
  component_members.clear();
  component_members.reserve(num_vertices);

  vector<SccInfo> scc_info(num_vertices);
  vector<DfsEntry> dfs_stack;
  vector<int> scc_stack;
  dfs_stack.reserve(num_vertices);
  scc_stack.reserve(num_vertices);
  int next_index = 0;

  REP(start_vertex, num_vertices) {
    if (scc_info[start_vertex].index != -1) continue;
    dfs_stack.push_back({start_vertex, -1});

    while (!dfs_stack.empty()) {
      DfsEntry dfs = dfs_stack.back();
      int v = dfs.v;
      dfs_stack.pop_back();

      if (dfs.next_edge == -1) {
        scc_info[v].index = next_index;
        scc_info[v].lowlink = next_index;
        ++next_index;
        scc_info[v].on_stack = true;
        scc_stack.push_back(v);
        dfs_stack.push_back({v, edges_start[v]});
      } else if (dfs.next_edge < edges_start[v+1]) {
        dfs_stack.push_back({v, dfs.next_edge+1});
        int w = edges[dfs.next_edge].to;
        if (scc_info[w].index == -1) {
          dfs_stack.push_back({w, -1});
        } else if (scc_info[w].on_stack) {
          scc_info[v].lowlink = min(scc_info[v].lowlink, scc_info[w].index);
        }
      } else {
        if (!dfs_stack.empty()) {
          int p = dfs_stack.back().v;
          scc_info[p].lowlink = min(scc_info[p].lowlink, scc_info[v].lowlink);
        }
        if (scc_info[v].lowlink == scc_info[v].index) {
          vector<int> new_component_members;
          for (;;) {
            assert(!scc_stack.empty());
            int w = scc_stack.back();
            scc_stack.pop_back();
            new_component_members.push_back(w);
            in_component[w] = num_components;
            scc_info[w].on_stack = false;
            if (w == v) break;
          }
          component_members.push_back(move(new_component_members));
          ++num_components;
        }
      }
    }
  }
}

struct LakeRange {
  int start;
  int len;
};

bool operator<(const LakeRange &a, const LakeRange &b) {
  if (a.start != b.start) return a.start < b.start;
  return a.len < b.len;
}

bool operator==(const LakeRange &a, const LakeRange &b) {
  return a.start == b.start && a.len == b.len;
}

vector<LakeRange> component_lake_range;

void calc_component_lake_range() {
  component_lake_range.resize(num_components);

  vector<LakeRange> ranges, reduced_ranges;
  ranges.reserve(edges.size() + num_lake);

  FORD(component, num_components-1, 0) {
    ranges.clear();
    for (int v : component_members[component]) {
      if (v < num_lake) ranges.push_back(LakeRange{v, 1});
      FOR(e, rev_edges_start[v], rev_edges_start[v+1]-1) {
        int w = rev_edges[e].to;
        int component2 = in_component[w];
        if (component2 == component) continue;
        const LakeRange &r = component_lake_range[component2];
        if (r.len == 0) continue;
        ranges.push_back(r);
      }
    }

    sort(ranges.begin(), ranges.end());

    reduced_ranges.clear();
    for (const LakeRange &r : ranges) {
      if (reduced_ranges.empty()) {
        reduced_ranges.push_back(r);
      } else {
        LakeRange &last_r = reduced_ranges.back();
        if (last_r.start + last_r.len >= r.start) {
          last_r.len = max(last_r.len, r.start + r.len - last_r.start);
        } else {
          reduced_ranges.push_back(r);
        }
      }
    }

    if (reduced_ranges.empty()) {
      component_lake_range[component] = LakeRange{0,0};
    } else if (reduced_ranges.size() == 1) {
      component_lake_range[component] = reduced_ranges[0];
    } else {
      assert(reduced_ranges.size() == 2);
      const LakeRange &r0 = reduced_ranges[0];
      const LakeRange &r1 = reduced_ranges[1];
      assert(r1.start + r1.len - num_lake >= r0.start);
      component_lake_range[component] =
        LakeRange{r1.start, max(r1.len, r0.start + num_lake + r0.len - r1.start)};
    }
    if (component_lake_range[component].len >= num_lake) {
      component_lake_range[component] = LakeRange{0,num_lake};
    }
  }
}

struct MultiLakeRange {
  LakeRange r;
  int count;
};

inline bool operator<(const MultiLakeRange &a, const MultiLakeRange &b) {
  return a.r < b.r;
}

Mod count_solutions() {
  vector<Mod> powers_of_2(num_ocean+1);
  powers_of_2[0] = 1;
  FOR(i,1,num_ocean) powers_of_2[i] = powers_of_2[i-1] + powers_of_2[i-1];

  vector<MultiLakeRange> ranges;
  ranges.reserve(num_ocean);
  int num_unconnected = 0;
  int num_full = 0;

  REP(component, num_components) {
    MultiLakeRange range;
    range.r = component_lake_range[component];
    range.count = 0;
    for (int v : component_members[component]) {
      if (v >= num_lake && v < num_lake + num_ocean) ++range.count;
    }

    if (range.count == 0) continue;

    if (range.r.len == 0) num_unconnected += range.count;
    else if (range.r.len >= num_lake) num_full += range.count;
    else ranges.push_back(range);
  }
  sort(ranges.begin(), ranges.end());

  // compress
  {
    int next = 0;
    for (const MultiLakeRange &range : ranges) {
      if (next > 0 && ranges[next-1].r == range.r) {
        ranges[next-1].count += range.count;
      } else {
        ranges[next] = range;
        ++next;
      }
    }
    ranges.resize(next);
  }

  int n = ranges.size();

  Mod num_solutions = 0;

  // Count solutions excluding num_unconnected and num_full.
  if (n==0) {
    num_solutions = 1;
  } else {
    // distance to previous required if this one set
    vector<int> prev_distance(n);
    {
      int prev = 0;
      int wrap_idx = n;
      int wrap_lake = num_lake;
      REP(i, n) {
        while (ranges[prev].r.start + ranges[prev].r.len - wrap_lake < ranges[i].r.start) {
          ++prev;
          if (prev == n) { prev=0; wrap_idx=0; wrap_lake=0; }
        }
        prev_distance[i] = i-prev+wrap_idx;
      }
    }

    vector<Mod> cnt(n+1);
    vector<Mod> cnt_sum(n+2);

    int start_beta = min_element(prev_distance.begin(), prev_distance.end()) - prev_distance.begin();
    FOR(start_dist, 1, prev_distance[start_beta]) {
      int start = start_beta - start_dist;
      if (start < 0) start += n;
      // start is enabled
      // [start+1 ... start + start_dist) are not enabled
      // cnt[i] = what if we select start+i
      // cnt[0] doesn't count multiplicities, cnd[n] does
      cnt[0] = 1;
      cnt_sum[0] = 0;
      cnt_sum[1] = 1;
      int p = start;
      FOR(i,1,n) {
        ++p;
        if (p == n) p=0;

        if (i < start_dist) {
          cnt[i] = 0;
        } else {
          int dist = min(prev_distance[p], i);
          Mod poss = cnt_sum[i] - cnt_sum[i-dist];
          poss *= (powers_of_2[ranges[p].count] - 1);
          cnt[i] = poss;
        }
        
        cnt_sum[i+1] = cnt_sum[i] + cnt[i];
      }
      num_solutions += cnt[n];
    }
  }

  // Set unconnected to anything (still when num_full not used).
  num_solutions *= powers_of_2[num_unconnected];

  // Add anything with num_full non-empty.
  num_solutions += powers_of_2[num_ocean] - powers_of_2[num_ocean - num_full];

  return num_solutions;
}

int main() {
  read_input();
  calc_components();
  calc_component_lake_range();
  Mod res = count_solutions();
  cout << res.get() << "\n";
}