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
#include <cstdio>
#include <cstdlib>
#include <cctype>

#include <map>
#include <vector>
#include <utility>

struct clause_t
{
    bool active;
    int firstLiteralNum;
    std::vector<bool> literals;
};

static const int MAX_N = 1000 * 1000;
static const long long MODULUS = 1000 * 1000 * 1000 + 7;

int n;
char linebuf[16 * MAX_N];

std::vector<clause_t> clauses;
std::map<int, std::vector<int>> clausesForVariable;

// #define DEBUG

#ifdef DEBUG
#define dpc(c) putchar(c)
#else
#define dpc(c) {}
#endif

void loadData()
{
    fgets(linebuf, sizeof(linebuf), stdin);
    sscanf(linebuf, "%d", &n);

    // printf("%d variables\n", n);

    fgets(linebuf, sizeof(linebuf), stdin);
    const char * bufp = linebuf;

    while (*bufp != '\n') {
        std::map<int, bool> literals;
        bufp++; // Skip '('
        dpc('(');

        while (*bufp != ')') {
            bool inverted = false;
            int varNum;
            
            if (*bufp == '~') {
                inverted = true;
                bufp++; // Skip '~'
                dpc('~');
            }

            bufp++; // Skip 'x'
            dpc('x');
            sscanf(bufp, "%d", &varNum);
            varNum--;
            while (isdigit(*bufp)) {
                bufp++;
            }
#ifdef DEBUG
            printf("%d", varNum);
#endif
            literals[varNum] = !inverted;

            if (*bufp == ' ') {
                bufp += 3; // Skip " v "
                dpc(' ');
                dpc('v');
                dpc(' ');
            }
        }

        bufp++; // Skip ')'
        dpc(')');
        if (*bufp == ' ') {
            bufp += 3; // Skip " ^ "
                dpc(' ');
                dpc('^');
                dpc(' ');
        }

        clause_t c;
        c.active = true;
        c.firstLiteralNum = literals.begin()->first;
        c.literals.reserve(literals.size());
        for (const auto & p : literals) {
            c.literals.push_back(p.second);
        }

        const int position = clauses.size();
        clauses.emplace_back(std::move(c));

        for (const auto & p : literals) {
            clausesForVariable[p.first].emplace_back(position);
        }
    }
}

long long brute(int v)
{
    auto indent = [&]() {
        for (int i = 0; i < v; i++) {
            putchar(' ');
        }
    };

    if (v == n)
        return 1;

    // indent();
    // printf("Trying to assign variable %d\n", v);

    auto & myClauses = clausesForVariable[v];

    for (int id : myClauses) {
        if (clauses[id].active) {
            goto normal;
        }
    }

    // indent();
    // printf("No active clause contains this variable\n");
    return 2LL * brute(v + 1);
normal:


    std::vector<int> saved;
    saved.reserve(myClauses.size());

    auto assignValue = [&](bool value) -> long long {
        // indent();
        // printf("Assuming %d is %s\n", v, value ? "true" : "false");
        long long ret = 0;
        for (int id : myClauses) {
            auto & clause = clauses[id];
            if (!clause.active) {
                continue;
            }

            if (clause.literals[v - clause.firstLiteralNum] == value) {
                saved.push_back(id);
                clause.active = false;
            }
            else if (clause.firstLiteralNum + clause.literals.size() - 1 == v) {
                // This clause will never be satisfied, so abort
                // indent();
                // printf("Clause #%d will not be satisfied, rolling back\n", v);
                goto rollback;
            }
        }

        // indent();
        // printf("Successfully assigned\n");

        ret = brute(v + 1);
    rollback:
        for (int id : saved) {
            clauses[id].active = true;
        }

        saved.clear();
        return ret;
    };

    
    return (assignValue(true) + assignValue(false)) % MODULUS;
}

int main()
{
    loadData();
    printf("%lld\n", brute(0));

    return 0;
}