p00950 Infallibly Crack Perplexing Cryptarithm
Example

Input

ACM


Output

0
```python
from itertools import permutations
base = "=+-*()01"
s = input()
l = len(s)
mapping = {}
counter = {}
cnt = 0
for c in s:
    if c in base:
        continue
    if c not in mapping:
        mapping[c] = cnt
        cnt += 1
    v = mapping[c]
    counter[v] = counter.get(v, 0) + 1
if cnt > 8:
    print(0)
    exit(0)

def solve(read):
    cur = failed = 0
    def next():
        nonlocal cur
        cur += 1
    def error():
        nonlocal failed
        failed = 1
    def number():
        res = 0
        if read(cur) not in "01":
            error()
        first = 1
        while 1:
            c = read(cur)
            if c not in "01":
                break
            if not first and res == 0:
                error()
            res = (res << 1) ^ int(c)
            next() # "0" or "1"
            first = 0
        return res
    def factor():
        c = read(cur)
        if c == "-":
            next() # "-"
            return -factor()
        elif c == "(":
            next() # "("
            val = expr()
            if read(cur) != ")":
                error()
            next() # ")"
            return val
        return number()
    def term():
        res = 1
        while 1:
            res *= factor()
            c = read(cur)
            if c != "*":
                break
            next() # "*"
        return res
    def expr():
        res = 0
        op = "+"
        while 1:
            if op == "+":
                res += term()
            else:
                res -= term()
            c = read(cur)
            if c not in "+-":
                break
            next() # "+" or "-"
            op = c
        return res
    if sum(read(i) == "=" for i in range(l)) != 1:
        return 0
    lv = expr()
    next() # "="
    rv = expr()
    if not failed and cur == l:
        return lv == rv
    return 0

def get(b):
    def read(cur):
        if l <= cur:
            return "$"
        if s[cur] in base:
            return s[cur]
        return b[mapping[s[cur]]]
    return read

ans = 0
for b in permutations(base, cnt):
    ans += solve(get(b))
print(ans)
```