$min = (0..2**10-1).map do |x|
    if x == 0
        nil
    else
        (x ^ (x - 1)).to_s(2).size-1
    end
end

$max = (0..2**10-1).map do |x|
    if x == 0
        nil
    else
        x.to_s(2).size-1
    end
end

$fixed = {}
(0..9).each do |i|
    $fixed[1 << i] = 0
end

class Integer
    def min
        $min[self]
    end
    def max
        $max[self]
    end
end

def f(state)
    r_min = $right.map {|i| state[i].min}.inject(0) {|r, t| r * 10 + t}
    l_max = $left.map {|a| a.map {|i| state[i].max}.inject(0) {|r, t| r * 10 + t}}.inject(:+)
    return 0 if l_max < r_min

    r_max = $right.map {|i| state[i].max}.inject(0) {|r, t| r * 10 + t}
    l_min = $left.map {|a| a.map {|i| state[i].min}.inject(0) {|r, t| r * 10 + t}}.inject(:+)
    return 0 if r_max < l_min
    
    k = state.index {|s| ! $fixed[s]}
    return 1 unless k

    x = state[k]
    sum = 0
    (0..9).each do |i|
        next if x[i] == 0
        mask = (1 << i)
        a = state.map.with_index {|s, j| j == k ? mask : (s - (s & mask))}
        sum += f(a)
    end
    sum
end

loop do
    n = gets.to_i
    break if n == 0
    a = (1..n).map { gets.chomp }
    letters = a.join.split("").uniq
    l = letters.size
    nonzero = a.select {|t| t.size > 1}.map {|t| t[0]}.uniq
    letters = nonzero + (letters - nonzero)

    $right = a[-1].each_char.map {|c| letters.index(c)}
    $left = a[0..-2].map {|t| t.each_char.map {|c| letters.index(c)}}

    init_state = [(1 << 10) - 1] * l
    nonzero.each do |c|
        init_state[letters.index(c)] -= 1
    end

    p f(init_state)
end