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
// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));


using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (1e6)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

VI wek[maxn];
bool ok[maxn];
int vis[maxn];
int numer[maxn];
bool tak[maxn];
LL dp[maxn];
int t = 1;
int skad[maxn];
queue<int> q;

LL pot(LL a, LL w)
{
  if(!w) return 1;
  if(w & 1) return a * pot(a, w - 1) % mod;
  return pot(a * a % mod, w / 2);
}

void bfs(int x)
{
  t++;
  q.push(x);
  vis[x] = t;
  while(SZ(q))
  {
    x = q.front();
    q.pop();
    for(auto s : wek[x])
      if(vis[s] < t)
      {
        vis[s] = t;
        q.push(s);
      }
  }
}

int main()
{
	_upgrade
  int n, m, a, b;
  cin>>n>>m>>a>>b;
  for(int i = 1;i <= m;i++)
  {
    int l, p;
    string xd;
    cin>>l>>xd>>p;
    wek[l].pb(p);
    if(xd == "--")
      wek[p].pb(l);
  }
  for(int i = 1;i <= a;i++)
  {
    q.push(i);
    ok[i] = 1;
  }
  while(SZ(q))
  {
    int cur = q.front();
    q.pop();
    for(auto s : wek[cur])
      if(ok[s] == 0)
      {
        ok[s] = 1;
        q.push(s);
      }
  }
  LL res = 1LL;
  int num = 1;
  for(int i = a + 1;i <= a + b;i++)
  {
    // dbg(i, ok[i]);
    if(ok[i] == 0)
      res = (res * 2LL) % mod;
    else
    {
      skad[num] = i;
      numer[i] = num++;
    }
  }
  num--;
  VPII V;
  for(int i = 1;i <= a;i++)
  {
    bfs(i);
    int l = -1;
    int p = -1;
    bool okok = 1;
    // dbg(i);
    for(int j = 1;j <= num;j++)
      tak[j] = (vis[skad[j]] == t);
    for(int j = 1;j <= num;j++)
    {
      // dbg(j, tak[j]);
      okok &= tak[j];
      if(tak[j] and (!tak[j == num ? 1 : j + 1]))
        p = j;
      if(tak[j] and (!tak[(j == 1 ? num : j - 1)]))
        l = j;
    }
    // dbg(l, p);
    if(l > p)
      p += num;
    // dbg(l, p);
    if(!okok)
      V.pb({l, p});
  }
  if(SZ(V) == 0)
    res *= (pot(2, num) - 1 + mod) % mod;
  else
  {
    sort(all(V));
    int mini = num;
    for(auto s : V)
      mini = min(mini, s.se);
    // for(auto s : V)
      // dbg(s.fi, s.se);
    LL tmp = 0LL;
    for(int pocz = 1;pocz <= mini;pocz++)
    {
      for(int i = 1;i <= num;i++)
        dp[i] = 0;
      dp[pocz] = 1;
      int wsk = 0;
      for(int i = pocz + 1;i <= num;i++)
      {
        wsk = max(0, wsk);
        while(wsk < SZ(V) and V[wsk].se < i)
          wsk++;
        wsk--;
        dp[i] = (dp[i - 1] - (wsk >= 0 ? dp[V[wsk].fi - 1] : 0));
        dp[i] = (dp[i] + dp[i - 1]) % mod;
      }
      // dbg(pocz);
      // for(int i = 1;i <= num;i++)
        // dbg(i, dp[i]);
      int skad = 1;
      for(auto s : V)
        if((!(s.fi <= pocz and pocz <= s.se)) and (!(s.fi <= pocz + num and pocz + num <= s.se)))
          skad = max(skad, s.fi);
      // dbg(skad, dp[num] - dp[skad - 1]);
      tmp = (tmp + dp[num] - dp[skad - 1]) % mod;
    }
    res *= tmp;
  }
  res %= mod;
  res += mod;
  res %= mod;
  cout<<res;
	return 0;
}