Repository: sympy/sympy

Issue:
collec with evaluate=False has leftover Dummy
For non-trivial expressions, `collect` returns incorrect dictionary with temporary `Dummy` leftover in the factor.

```
>> from sympy import symbol, collect
>> a = symbols('a')
>> collect(a**2*(a**2 + 1), a**2, evaluate=False)
{a**2: _Dummy_20 + 1}
```

Discussion:
sympy-bot: 

Hi, I am the [SymPy bot](https://github.com/sympy/sympy-bot) (v158). I'm here to help you write a release notes entry. Please read the [guide on how to write release notes](https://github.com/sympy/sympy/wiki/Writing-Release-Notes).

:x: There was an issue with the release notes. **Please do not close this pull request;** instead edit the description after reading the [guide on how to write release notes](https://github.com/sympy/sympy/wiki/Writing-Release-Notes).

* No release notes were found. Please edit the PR description and write the release notes under `<!-- BEGIN RELEASE NOTES -->`.

Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. <details><summary>Click here to see the pull request description that was parsed.</summary>

    <!-- Your title above should be a short description of what
    was changed. Do not include the issue number in the title. -->

    #### References to other Issues or PRs
    <!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
    format, e.g. "Fixes #1234" (see
    https://tinyurl.com/auto-closing for more information). Also, please
    write a comment on that issue linking back to this pull request once it is
    open. -->


    #### Brief description of what is fixed or changed

    This PR  fixes a small issue in `collect` with the collected symbol in the factor. Current version does:
    ```
    >> from sympy import symbol, collect
    >> a = symbols('a')
    >> collect(a**2*(a**2 + 1), a**2, evaluate=False)
    {a**2: _Dummy_20 + 1}
    ```
    that contains an undefined `Dummy` in the collected expression,. This fixes the behavior applying replacement to the factors and outputs in this example.
    ```
    {a**2: a**2 + 1}
    ```
    #### Other comments


    #### Release Notes

    <!-- Write the release notes for this release below. See
    https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more information
    on how to write release notes. The bot will check your release notes
    automatically to see if they are formatted correctly. -->

    <!-- BEGIN RELEASE NOTES -->

    <!-- END RELEASE NOTES -->

</details><p>


Patch:
diff --git a/sympy/simplify/radsimp.py b/sympy/simplify/radsimp.py
index 9457921008..b013f6b926 100644
--- a/sympy/simplify/radsimp.py
+++ b/sympy/simplify/radsimp.py
@@ -178,7 +178,7 @@ def collect(expr, syms, func=None, evaluate=None, exact=False, distribute_order_
         if not isinstance(rv, dict):
             return rv.xreplace(urep)
         else:
-            return {urep.get(k, k): v for k, v in rv.items()}
+            return {urep.get(k, k): v.xreplace(urep) for k, v in rv.items()}
 
     if evaluate is None:
         evaluate = global_parameters.evaluate


Test patch:
diff --git a/sympy/simplify/tests/test_radsimp.py b/sympy/simplify/tests/test_radsimp.py
index e56fd14f82..769ddcfaff 100644
--- a/sympy/simplify/tests/test_radsimp.py
+++ b/sympy/simplify/tests/test_radsimp.py
@@ -223,6 +223,12 @@ def test_collect_5():
                    [x, y]) == 1 + y + x*(1 + 2*y) + x**2 + y**2
 
 
+def test_collect_pr19431():
+    """Unevaluated collect with respect to a product"""
+    a = symbols('a')
+    assert collect(a**2*(a**2 + 1), a**2, evaluate=False)[a**2] == (a**2 + 1)
+
+
 def test_collect_D():
     D = Derivative
     f = Function('f')