Problem:
Testing Circuits

A Boolean expression is given.
In the expression, each variable appears exactly once.
Calculate the number of variable assignments that make the expression evaluate to true.

Input

A data set consists of only one line.
The Boolean expression is given by a string which consists of digits, x, (, ), |, &, and ~.
Other characters such as spaces are not contained.
The expression never exceeds 1,000,000 characters.
The grammar of the expressions is given by the following BNF.

<expression> ::= <term> | <expression> "|" <term>
<term> ::= <factor> | <term> "&" <factor>
<factor> ::= <variable> | "~" <factor> | "(" <expression> ")"
<variable> ::= "x" <number>
<number> ::= "1" | "2" |... | "999999" | "1000000"

The expression obeys this syntax and thus you do not have to care about grammatical errors.
When the expression contains N variables, each variable in {x1, x2,..., xN} appears exactly once.

Output

Output a line containing the number of variable assignments that make the expression evaluate to true in modulo 1,000,000,007.

Sample Input 1

(x1&x2)

Output for the Sample Input 1

1

Sample Input 2

(x1&x2)|(x3&x4)|(~(x5|x6)&(x7&x8))

Output for the Sample Input 2

121

Solution in C++ (judge verdict: Runtime Error)

/*
 * 2348.cc: Testing Circuits
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;

typedef long long ll;
const ll MOD = 1000000007;

/* typedef */

typedef pair<ll,ll> pll;

/* global variables */

/* subroutines */

// <expression> ::= <term> | <expression> "|" <term>
// <term> ::= <factor> | <term> "&" <factor>
// <factor> ::= <variable> | "~" <factor> | "(" <expression> ")"
// <variable> ::= "x" <number>
// <number> ::= "1" | "2" |... | "999999" | "1000000"
		     
pll expr(string &s, int &pos);
pll term(string &s, int &pos);
pll factor(string &s, int &pos);

pll expr(string &s, int &pos) {
  pll e = term(s, pos);
  while (pos < s.size() && s[pos] == '|') {
    pos++; // '|'
    pll t = term(s, pos);
    e.second =
      (e.second * t.second % MOD +
       e.second * (t.first + MOD - t.second) % MOD +
       (e.first + MOD - e.second) * t.second % MOD) % MOD;
    e.first = e.first * t.first % MOD;
  }
  return e;
}

pll term(string &s, int &pos) {
  pll t = factor(s, pos);
  while (pos < s.size() && s[pos] == '&') {
    pos++; // '&'
    pll f = factor(s, pos);
    t.second = t.second * f.second % MOD;
    t.first = t.first * f.first % MOD;
  }
  return t;
}

pll factor(string &s, int &pos) {
  if (s[pos] == 'x') {
    pos++; // 'x'
    while (pos < s.size() && s[pos] >= '0' && s[pos] <= '9') pos++;
    return pll(2, 1);
  }

  if (s[pos] == '~') {
    pos++; // '~'
    pll f = factor(s, pos);
    f.second = (f.first + MOD - f.second) % MOD;
    return f;
  }
  
  pos++; // '('
  pll e = expr(s, pos);
  pos++; // ')'
  return e;
}

/* main */

int main() {
  string s;
  cin >> s;

  int pos = 0;
  pll e = expr(s, pos);

  printf("%lld\n", e.second);
  return 0;
}