Handshakes That Don't Cross Problem
Description
LeetCode Problem 1259.
You are given an even number of people numPeople that stand around a circle and each person shakes hands with someone else so that there are numPeople / 2 handshakes total.
Return the number of ways these handshakes could occur such that none of the handshakes cross.
Since the answer could be very large, return it modulo 10^9 + 7.
Example 1:

Input: numPeople = 4
Output: 2
Explanation: There are two ways to do it, the first way is [(1,2),(3,4)] and the second one is [(2,3),(4,1)].
Example 2:

Input: numPeople = 6
Output: 5
Constraints:
2 <= numPeople <= 1000numPeopleis even.
Solution
Approach: Dynamic Programming on the Number of Pairs
Look at person 1. They shake hands with some person j, and that handshake is a chord that cuts the circle in two. Since no handshakes may cross it, the people on one side of the chord can only shake hands among themselves, and the same for the other side. Each side must then have an even number of people, and each side is a smaller copy of the same problem.
Let ways[k] be the answer for 2k people (k pairs), with ways[0] = 1 (nobody left, one way: do nothing). If person 1's handshake leaves left pairs on one side, the other side has k - 1 - left pairs, and the two sides are independent, so
ways[k] = sum over left = 0 .. k - 1 of ways[left] × ways[k - 1 - left].
These are the Catalan numbers: 1, 1, 2, 5, 14, 42, ... For 6 people, person 1 can leave 0 + 2, 1 + 1 or 2 + 0 pairs, giving 2 + 1 + 2 = 5.
The values grow huge, so we reduce modulo 109 + 7 after every step. A product of two values below the modulus needs 64 bits (and BigInt in JavaScript, where a double would lose precision).
Complexity: O(n2) time and O(n) space, where n is numPeople.
class Solution:
def numberOfWays(self, numPeople: int) -> int:
MOD = 10**9 + 7
pairs = numPeople // 2
# ways[k]: the answer for 2k people. Person 1's handshake splits the rest into two separate
# groups, with `left` pairs on one side and k - 1 - left on the other.
ways = [1] + [0] * pairs
for k in range(1, pairs + 1):
for left in range(k):
ways[k] = (ways[k] + ways[left] * ways[k - 1 - left]) % MOD
return ways[pairs]