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
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=500010,M=2000010,P=1000000007;
int n,m,ca,cb,cnt,i,j,k,x,y,scc,base=1;
bool vis[N];
int ed,g[N],v[M],nxt[M],G[N],V[M],NXT[M];
int id[N],at[N],q[N],t;
int pool[N],st[N],en[N],cp,last[N];
int ce;
struct Info{int a,b,c,d;}f[N];
struct E{int l,r;E(){}E(int _l,int _r){l=_l,r=_r;}}e[N<<1];
inline bool cmp(const E&a,const E&b){return a.l<b.l;}
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline void readch(char&a){while(!(((a=getchar())=='-')||(a=='>')));}
inline void umin(int&a,int b){a>b?(a=b):0;}
inline void umax(int&a,int b){a<b?(a=b):0;}
inline void add(int x,int y){
  v[++ed]=y;nxt[ed]=g[x];g[x]=ed;
  V[ed]=x;NXT[ed]=G[y];G[y]=ed;
}
void dfs(int x){
  if(vis[x])return;
  vis[x]=1;
  for(int i=g[x];i;i=nxt[i])dfs(v[i]);
}
void dfs1(int x){
  if(vis[x])return;
  vis[x]=1;
  for(int i=g[x];i;i=nxt[i])dfs1(v[i]);
  q[++t]=x;
}
void dfs2(int x){
  if(!vis[x])return;
  vis[x]=0;
  at[x]=scc;
  pool[++cp]=x;
  for(int i=G[x];i;i=NXT[i])dfs2(V[i]);
}
inline void ext(int o,int x,int y){
  if(!x)return;
  if(!f[o].a)f[o].a=x,f[o].b=y;
  else if(!f[o].c)f[o].c=x,f[o].d=y;
  else puts("GG");
}
namespace COUNTING{
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
typedef uint32 word;
typedef uint64 dword;
typedef int sword;
const int word_bits=sizeof(word)*8;
word mod,Modinv,r2;
struct UnsafeMod{
  word x;
  UnsafeMod(): x(0) {}
  UnsafeMod(word _x): x(init(_x)) {}
  UnsafeMod& operator += (const UnsafeMod& rhs) {
    (x += rhs.x) >= mod && (x -= mod);
    return *this;
  }
  UnsafeMod& operator -= (const UnsafeMod& rhs) {
    sword(x -= rhs.x) < 0 && (x += mod);
    return *this;
  }
  UnsafeMod& operator *= (const UnsafeMod& rhs) {
    x = reduce(dword(x) * rhs.x);
    return *this;
  }
  UnsafeMod operator + (const UnsafeMod &rhs) const {
    return UnsafeMod(*this) += rhs;
  }
  UnsafeMod operator - (const UnsafeMod &rhs) const {
    return UnsafeMod(*this) -= rhs;
  }
  UnsafeMod operator * (const UnsafeMod &rhs) const {
    return UnsafeMod(*this) *= rhs;
  }
  UnsafeMod pow(uint64 e) const {
    UnsafeMod ret(1);
    for (UnsafeMod base = *this; e; e >>= 1, base *= base) {
      if (e & 1) ret *= base;
    }
    return ret;
  }
  word get() const {
    return reduce(x);
  }
  static word modulus() {
    return mod;
  }
  static word init(word w) {
    return reduce(dword(w) * r2);
  }
  static void set_mod(word m) {
    mod = m;
    Modinv = mul_inv(mod);
    r2 = -dword(mod) % mod;
  }
  static word reduce(dword x) {
    word y = word(x >> word_bits) - word((dword(word(x) * Modinv) * mod) >> word_bits);
    return sword(y) < 0 ? y + mod : y;
  }
  static word mul_inv(word n, int e = 6, word x = 1) {
    return !e ? x : mul_inv(n, e - 1, x * (2 - x * n));
  }
}dp[N],v[N],ans;
int n,m,_,ca,i,j,k,x,y,e[N][2],is[N],a[N];
int p[N],val[N];
int minlen,S,now,en;
int g[N],q[N][2],h,t;
int f[N<<1],lim[N];
inline void init(int A,int B,int C,int D){
  //printf("init %d %d %d %d\n",A,B,C,D);
  if(!C){
    //[A,B]
    e[++m][0]=A;
    e[m][1]=B;
    umax(f[B],A);
    umax(f[B+n],A+n);
  }else{
    //[C,B]
    e[++m][0]=C;
    e[m][1]=B;
    umax(f[B+n],C);
  }
}
inline void add(int l,int r){
  //printf("add %d %d\n",l,r);
  if(l<=r){
    if(f[r]>l)return;
    is[l-1]=is[r]=1;
  }else{
    if(f[r+n]>l)return;
    is[l-1]=is[r]=is[n]=1;
  }
  e[++_][0]=l;
  e[_][1]=r;
}
inline void ins(int x,int y){
  if(h<=t&&q[t][1]>=y)return;
  q[++t][0]=x;
  q[t][1]=y;
}
inline int askmin(int x){
  while(h<=t&&q[h][1]<=x)h++;
  if(h>t)return n;
  return q[h][0];
}
inline void cal(int lim,int n){
  for(int i=1;i<=n;i++){
    dp[i]=dp[i-1];
    if(f[i-1])dp[i]-=dp[f[i-1]-1];
    if(i<=lim)dp[i]+=1;
    dp[i]=dp[i]*v[i]+dp[i-1];
  }
}
int solve(){
  UnsafeMod::set_mod(P);
  for(i=1;i<=n+n;i++)umax(f[i],f[i-1]);
  for(i=1;i<=m;i++)add(e[i][0],e[i][1]);
  m=_;
  for(p[0]=i=1;i<=n;i++)p[i]=p[i-1]*2%P;
  for(i=0;i<=n;i++)p[i]--;
  is[n]=1;
  for(i=1;i<=n;i++)if(is[i])a[is[i]=++ca]=i;
  for(i=n;i;i--)if(!is[i])is[i]=is[i+1];
  //[a[i-1]+1..a[i]]
  n=ca;
  for(i=1;i<=n;i++)val[i]=p[a[i]-a[i-1]];
  //printf("n=%d\n",n);
  //for(i=1;i<=n;i++)printf("%d ",val[i]);puts("");
  minlen=n,S=1;
  for(i=1;i<=m;i++){
    x=is[e[i][0]],y=is[e[i][1]];
    //printf("%d %d\n",x,y);
    if(x<=y)now=y-x+1;else now=y-x+1+n;
    if(now<=minlen){
      minlen=now;
      S=x;
    }
  }
  //printf("minlen=%d S=%d\n",minlen,S);
  for(i=1,j=S;i<=n;i++){
    v[i]=val[j];
    j++;
    if(j>n)j=1;
  }
  en=n;
  for(i=0;i<=n+1;i++)f[i]=0;
  for(i=1;i<=m;i++){
    x=is[e[i][0]],y=is[e[i][1]];
    if(y<x)y+=n;
    y=y-x;
    while(1){
      if(S<=x&&x<S+n)break;
      x+=n;
    }
    x-=S-1;
    if(y==n-1)x=1,y=n;else y+=x;
    if(y>n)y-=n;
    //printf("-> %d %d\n",x,y);
    if(x<=y){
      //[x..y]
      umax(f[y],x);
      umin(en,y);
    }else{
      umax(g[y+1],x);
      //[1..y]   [x..n]
    }
  }
  for(i=1;i<=n;i++)umax(f[i],f[i-1]);
  h=1,t=0;
  for(i=1;i<=n;i++){//last
    now=en;//first<=lim
    if(g[i])ins(i-1,g[i]);
    umin(now,askmin(i));
    lim[i]=now;
  }
  for(i=max(f[n],1);i<=n;i=j){
    for(j=i;j<=n&&lim[i]==lim[j];j++);
    cal(lim[i],j-1);
    ans+=dp[j-1]-dp[i-1];
  }
  return ans.get();
}
}
int main(){
  read(n);read(m);read(ca);read(cb);
  while(m--){
    char ch;
    read(x);
    readch(ch);
    readch(ch);
    read(y);
    add(x,y);
    if(ch=='-')add(y,x);
  }
  for(i=1;i<=ca;i++)dfs(i);
  for(i=ca+1;i<=ca+cb;i++)if(vis[i])id[i]=++cnt;else base=base*2%P;
  for(i=1;i<=n;i++)vis[i]=0;
  for(i=1;i<=n;i++)if(!vis[i])dfs1(i);
  for(i=t;i;i--)if(vis[q[i]]){
    scc++;
    st[scc]=cp+1;
    dfs2(q[i]);
    en[scc]=cp;
  }
  for(i=scc;i;i--){
    ce=0;
    for(j=st[i];j<=en[i];j++){
      x=pool[j];
      if(id[x])e[++ce]=E(id[x],id[x]);
      for(k=g[x];k;k=nxt[k]){
        y=at[v[k]];
        if(y==i)continue;
        if(last[y]==i)continue;
        last[y]=i;
        if(f[y].a)e[++ce]=E(f[y].a,f[y].b);
        if(f[y].c)e[++ce]=E(f[y].c,f[y].d);
      }
    }
    if(!ce)continue;
    sort(e+1,e+ce+1,cmp);
    x=0,y=-1;
    for(j=1;j<=ce;j++){
      if(e[j].l>y+1){
        ext(i,x,y);
        x=e[j].l;
      }
      umax(y,e[j].r);
    }
    ext(i,x,y);
  }
  //puts("OK");
  COUNTING::n=cnt;
  for(i=1;i<=ca;i++){
    x=at[i];
    if(f[x].a){
      COUNTING::init(f[x].a,f[x].b,f[x].c,f[x].d);
      f[x].a=0;
    }
  }
  base=1LL*base*COUNTING::solve()%P;
  printf("%d",base);
}