def solve(letter)
  @memo = Hash.new{|h, k| h[k] = Hash.new{0}}
  dfs(letter.dup, 0, (0..9).to_a)
  @memo
end

def dfs(letter, sum, unused)
  return @memo[sum][(0..9).to_a - unused] += 1 if letter.empty?
  c = letter.shift
  unused.each do |i|
    next if i == 0 && @head[c]
    dfs(letter.dup, sum + @coefficient[c] * i, unused.select{|v| v != i})
  end
end

until (n = gets.to_i).zero?
  @head = Hash.new
  @coefficient = Hash.new{0}
  n.times do |i|
    string = gets.chomp
    @head[string.chr] = true if string.size > 1
    case i
      when n - 1
        @coefficient[string.slice!(0)] += 10 ** string.size until string.size.zero?
      else
        @coefficient[string.slice!(0)] -= 10 ** string.size until string.size.zero?
    end
  end

  former = solve(@coefficient.keys[0, @coefficient.size / 2])
  later = solve(@coefficient.keys[@coefficient.size / 2, @coefficient.size])
  answer = 0
  former.each do |sum, h|
    h.each do |used, count|
      later[-sum].each do |u, c|
        answer += count * c if (used & u).size == 0
      end
    end
  end
  p answer
end