<p>There are <code>n</code> uniquely-sized sticks whose lengths are integers from <code>1</code> to <code>n</code>. You want to arrange the sticks such that <strong>exactly</strong> <code>k</code>&nbsp;sticks are <strong>visible</strong> from the left. A stick&nbsp;is <strong>visible</strong> from the left if there are no <strong>longer</strong>&nbsp;sticks to the <strong>left</strong> of it.</p>

<ul>
	<li>For example, if the sticks are arranged <code>[<u>1</u>,<u>3</u>,2,<u>5</u>,4]</code>, then the sticks with lengths <code>1</code>, <code>3</code>, and <code>5</code> are visible from the left.</li>
</ul>

<p>Given <code>n</code> and <code>k</code>, return <em>the <strong>number</strong> of such arrangements</em>. Since the answer may be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 3, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> [<u>1</u>,<u>3</u>,2], [<u>2</u>,<u>3</u>,1], and [<u>2</u>,1,<u>3</u>] are the only arrangements such that exactly 2 sticks are visible.
The visible sticks are underlined.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 5, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> [<u>1</u>,<u>2</u>,<u>3</u>,<u>4</u>,<u>5</u>] is the only arrangement such that all 5 sticks are visible.
The visible sticks are underlined.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 20, k = 11
<strong>Output:</strong> 647427950
<strong>Explanation:</strong> There are 647427950 (mod 10<sup>9 </sup>+ 7) ways to rearrange the sticks such that exactly 11 sticks are visible.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
	<li><code>1 &lt;= k &lt;= n</code></li>
</ul>

class Solution:
    def rearrangeSticks(self, n: int, k: int) -> int:
        MOD = 10 ** 9 + 7
        # Precalculate all necessary modular inverses and factorial inverses from 1 to 2 * n - k
        upper_bound_precalc = 2 * n - k + 2
        inverses = [1] * (upper_bound_precalc + 1)
        inverse_factorials = [1] * (upper_bound_precalc + 1)

        for a in range(2, upper_bound_precalc + 1):
            inverses[a] = MOD - MOD // a * inverses[MOD % a] % MOD
            inverse_factorials[a] = (inverses[a] * inverse_factorials[a - 1]) % MOD

        # Precompute exponents
        total = 0
        my_pow_list = [pow(j, n - k, MOD) for j in range(n - k + 1)]

        for i in range(n - k + 1):
            multiplier = (inverses[n + i] * inverse_factorials[n - k - i] * inverse_factorials[n - k + i]) % MOD
            add_here = 0

            # Follow the math formula
            for j in range(i + 1):
                if j % 2 == 0:
                    add_here += my_pow_list[j] * inverse_factorials[j] * inverse_factorials[i - j]
                else:
                    add_here -= my_pow_list[j] * inverse_factorials[j] * inverse_factorials[i - j]

            # Update the powers list
            my_pow_list = [(j * my_pow_list[j]) % MOD for j in range(n - k + 1)]

            total += multiplier * add_here % MOD

        # Multiply by factorials, and flip the sign of answer if n != k mod 2
        total %= MOD
        total *= pow(inverse_factorials[2 * n - k], -1, MOD) * inverse_factorials[k - 1] * ((-1) ** ((n - k) % 2))

        return total % MOD
267