------
## 最终结论
```python
def numDistinct(S: str, T: str) -> int:
    m, n = len(S), len(T)
    
    # Create a 2D DP array with (m+1) x (n+1) dimensions
    dp = [[0] * (n + 1) for _ in range(m + 1)]
    
    # Base case initialization
    for i in range(m + 1):
        dp[i][0] = 1
    
    # Fill the DP table
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if S[i - 1] == T[j - 1]:
                dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
            else:
                dp[i][j] = dp[i - 1][j]
    
    # Return the result
    return dp[m][n]

# Example usage:
S = "rabbbit"
T = "rabbit"
print(numDistinct(S, T))  # Output: 3

S = "babgbag"
T = "bag"
print(numDistinct(S, T))  # Output: 5
```