def multiproduct(seq=(), start=1):
 if (not seq):
  return start
 if isinstance(seq, dict):
  seq = iter(seq.items())
 units = start
 multi = []
 for (base, exp) in seq:
  if (not exp):
   continue
  elif (exp == 1):
   units *= base
  else:
   if (exp % 2):
    units *= base
   multi.append((base, (exp // 2)))
 return (units * (multiproduct(multi) ** 2))

def multiproduct ( seq = ( ) , start = 1 ) : if ( not seq ) : return start if isinstance ( seq , dict ) : seq = iter ( seq . items ( ) ) units = start multi = [ ] for ( base , exp ) in seq : if ( not exp ) : continue elif ( exp == 1 ) : units *= base else : if ( exp % 2 ) : units *= base multi . append ( ( base , ( exp // 2 ) ) ) return ( units * ( multiproduct ( multi ) ** 2 ) )
return the product of a sequence of factors with multiplicities .