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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

import static java.lang.Math.abs;
import static java.lang.Math.max;

public class cnf {
    private static final long B = 1_000_000_007L;
    BufferedReader rd;

    cnf() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        int n = pint();
        String s = rd.readLine();
        out(compute(n,parse(s)));
    }

    private List<List<Integer>> parse(String s) {
        List<List<Integer>> res = new ArrayList<>();
        String[] h = s.split("\\^");
        int m = h.length;
        for(int i=0;i<m;i++) {
            String[] u = h[i].split("[^0-9x~]+");
            List<Integer> res2 = new ArrayList<>();
            res.add(res2);
            for(String lit: u) {
                lit = lit.trim();
                if(!lit.isEmpty()) {
                    int plit = pint(lit.replaceAll("[^0-9]+",""));
                    if(lit.charAt(0) == '~') {
                        plit *= -1;
                    }
                    res2.add(plit);
                }
            }
        }
        return res;
    }

    private long compute(int n, List<List<Integer>> in) {
        List<Set<Integer>> endingAt = new ArrayList<>();
        for(int i=0;i<n;i++) {
            endingAt.add(new HashSet<>());
        }
        List<List<Set<Integer>>> a = new ArrayList<>();
        for(int j=0;j<2;j++) {
            List<Set<Integer>> z = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                z.add(new HashSet<>());
            }
            a.add(z);
        }
        int m = in.size();
        for(int i=0;i<m;i++) {
            List<Integer> u = in.get(i);
            int mx = 0;
            for(Integer lit: u) {
                int aIx = lit < 0?1:0;
                int plit = abs(lit) - 1;
                mx = max(mx,plit);
                a.get(aIx).get(plit).add(i);
            }
            endingAt.get(mx).add(i);
        }

        long[] p2 = new long[n+1];
        p2[n] = 1;
        for(int i=0;i<n;i++) {
            p2[n-1-i] = p2[n-i] << 1;
            p2[n-1-i] %= B;
        }

        Set<Integer> tm = new HashSet<>();
        for(int j=0;j<m;j++) {
            tm.add(j);
        }
        Deque<Node> d = new ArrayDeque<>();
        d.push(new Node(0, 0, new HashSet<>(tm)));
        long res = 0;
        while(!d.isEmpty()) {
            Node node = d.peek();
            if(node.nextValue > 1) {
                d.pop();
            } else {
                Set<Integer> sat = a.get(node.nextValue).get(node.pos);
                Set<Integer> nn = new HashSet<>(node.avail);
                nn.removeAll(sat);
                if (nn.isEmpty()) {
                    res += p2[node.pos + 1];
                    res %= B;
                    if (node.nextValue == 1) {
                        d.pop();
                    } else {
                        node.nextValue++;
                    }
                } else {
                    node.nextValue++;
                    boolean ok = node.pos + 1 < n;
                    if (ok) {
                        for (Integer endingHere : endingAt.get(node.pos)) {
                            if (nn.contains(endingHere)) {
                                ok = false;
                                break;
                            }
                        }
                        if (ok) {
                            d.push(new Node(node.pos + 1, 0, nn));
                        }
                    }
                    if (!ok) {
                        if (node.nextValue > 1) {
                            d.pop();
                        }
                    }
                }
            }
        }
        return res;
    }

    private class Node {
        private final int pos;
        private int nextValue;
        private final Set<Integer> avail;

        public Node(int pos, int nextValue, Set<Integer> avail) {
            this.pos = pos;
            this.nextValue = nextValue;
            this.avail = avail;
        }
    }

    private int pint() throws IOException {
        return pint(rd.readLine());
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new cnf();
    }
}