'Returns True if the expression needs to be wrapped in brackets when
printed as part of a Mul, False otherwise. This is True for Add,
but also for some container objects that would not need brackets
when appearing last in a Mul, e.g. an Integral. ``last=True``
specifies that this expr is the last to appear in a Mul.
``first=True`` specifies that this expr is the first to appear in a Mul.'

def _needs_mul_brackets(self, expr, first=False, last=False):

    from sympy import Integral, Piecewise, Product, Sum
    if expr.is_Mul:
        if ((not first) and _coeff_isneg(expr)):
            return True
    elif (precedence_traditional(expr) < PRECEDENCE['Mul']):
        return True
    elif expr.is_Relational:
        return True
    if expr.is_Piecewise:
        return True
    if any([expr.has(x) for x in (Mod,)]):
        return True
    if ((not last) and any([expr.has(x) for x in (Integral, Product, Sum)])):
        return True
    return False
