Attack of the Clones

All submissions for this problem are available.
A boolean function is a function of the form f: B_{n} > B, where B = {0, 1} and n is a nonnegative integer called the arity of the function. Some Boolean functions are projections: p_{n}^{k}(x_{1}, ..., x_{n}) = x_{k}. And given an mary function f, and nary functions g_{1}, ..., g_{m}, we can construct another nary function: h(x_{1}, ..., x_{n}) = f(g1(x_{1}, ..., x_{n}), ..., gm(x_{1}, ..., x_{n})), called their composition. A set of functions closed under composition and containing all projections is called a clone. One trivial clone is a set of all boolean functions. Some of the special clones are:
 Z is a set of 0preserving functions: f(0, ..., 0) = 0;
 P is a set of 1preserving functions: f(1, ..., 1) = 1;
 D is a set of selfdual functions: !f(x_{1}, ..., x_{n}) = f(!x_{1}, ..., !x_{n});
 A is a set of affine functions: the functions satisfying that if f(a_{1}, ..., c, ..., a_{n}) = f(a_{1}, ..., d, ..., a_{n}) then f(b_{1}, ..., c, ..., b_{n}) = f(b_{1}, ..., d, ..., b_{n}), where c and d are at some position i. This should hold for every valid i, a_{1}, ..., a_{n}, b_{1}, ... b_{n}, c and d.
Now we are interested how many nary functions are there in some combinations of mentioned above sets. For example, for n = 2, there are exactly 8 functions in Z, 4 functions in the intersection of Z and P, 8 function in the complement of A and so on.
Input
The first line of the input file contains n  the arity of the boolean functions we are looking at. The second line contains the q  number of queries. Each of the next q lines will describe a query. The query is a set expression. The expression will contain the following characters: 'Z', 'P', 'D', 'A' denoting the sets, described above; 'v'  which is set union; '^'  which is set intersection; '!' which is complement; '\' which is set difference; and also '(' and ')' to define operations priority. Operations in brackets have higher priority. Otherwise the '!' operation has the higher priority and 'v', '^' and '\' are of the same priority. It is guaranteed that the expression will be correct. See samples for some examples of set expressions.
Constraints
1 <= n <= 100
1 <= q <= 100
The length of each expression won't exceed 100 characters.
Output
For each query in the input print how many nary function are in the set described by the according set expression modulo 1000003.
Example
Input: 2 6 Z Z^P !A !(AvP)^D AvZvP\A !A^(Z\(Dv!P)) Output: 8 4 8 0 6 2
Author:  spooky 
Tester:  gamabunta 
Editorial  http://discuss.codechef.com/problems/CLONES 
Tags  hard, june11, spooky 
Date Added:  17042011 
Time Limit:  0.111111 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 