> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Binary Trees With Factors Python Solution

> Tested Python solution for LeetCode 823 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 823, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Dynamic Programming](/catalog/topics/dynamic-programming), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/binary-trees-with-factors/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 823   # by problem number
lcpy gen -s binary_trees_with_factors   # by problem name
```

## Problem

Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.

We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.

Return *the number of binary trees we can make*. The answer may be too large so return the answer **modulo** `10^9 + 7`.

### Examples

```
Input: arr = [2,4]
Output: 3
Explanation: We can make these trees: [2], [4], [4, 2, 2]
```

```
Input: arr = [2,4,5,10]
Output: 7
Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
```

### Constraints

* `1 <= arr.length <= 1000`
* `2 <= arr[i] <= 10^9`
* All the values of `arr` are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_trees_with_factors/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_trees_with_factors/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(n)
    def num_factored_binary_trees(self, arr: list[int]) -> int:
        mod = 10**9 + 7
        vals = sorted(arr)
        index = {v: i for i, v in enumerate(vals)}
        count_for: dict[int, int] = {}
        total = 0
        for i, v in enumerate(vals):
            ways = 1
            for j in range(i):
                if v % vals[j]:
                    continue
                complement = v // vals[j]
                if complement in index and index[complement] < i:
                    ways += count_for[vals[j]] * count_for[complement]
            count_for[v] = ways
            total += ways
        return total % mod
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.