<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/e.png" style="width: 424px; height: 372px;" />
<p>You should build the array arr which has the following properties:</p>

<ul>
	<li><code>arr</code> has exactly <code>n</code> integers.</li>
	<li><code>1 &lt;= arr[i] &lt;= m</code> where <code>(0 &lt;= i &lt; n)</code>.</li>
	<li>After applying the mentioned algorithm to <code>arr</code>, the value <code>search_cost</code> is equal to <code>k</code>.</li>
</ul>

<p>Return <em>the number of ways</em> to build the array <code>arr</code> under the mentioned conditions. As the answer may grow large, the answer <strong>must be</strong> computed modulo <code>10<sup>9</sup> + 7</code>.</p>

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

<pre>
<strong>Input:</strong> n = 2, m = 3, k = 1
<strong>Output:</strong> 6
<strong>Explanation:</strong> The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]
</pre>

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

<pre>
<strong>Input:</strong> n = 5, m = 2, k = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no possible arrays that satisfy the mentioned conditions.
</pre>

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

<pre>
<strong>Input:</strong> n = 9, m = 1, k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 50</code></li>
	<li><code>1 &lt;= m &lt;= 100</code></li>
	<li><code>0 &lt;= k &lt;= n</code></li>
</ul>

// class Solution {
// public:
//     int mod=1e9+7;
//     int dp[51][101][51];
//     int helper(int idx,int search_cost,int maxi, int n,int m,int k){
//         //base case
//         if(idx==n){
//             if(search_cost==k){
//                 return 1;
//             }
//             return 0;
//         }
//         if(dp[idx][maxi][search_cost]!=-1){
//             return dp[idx][maxi][search_cost];
//         }

//         int result=0;
//         for(int j=1;j<=m;j++){
//            if(j>maxi){
//             result=(result+(helper(idx+1,search_cost+1,j,n,m,k))%mod)%mod;
//            }
//            else{
//             result=(result+(helper(idx+1,search_cost,maxi,n,m,k))%mod)%mod;
//            }
//         }

//         return dp[idx][maxi][search_cost]=result%mod ;
//     }
//     int numOfArrays(int n, int m, int k) {
//         memset(dp,-1,sizeof(dp));
//         return helper(0,0,0,n,m,k);
//     }
// };

// class Solution {
// public:
//     int numOfArrays(int n, int m, int k) {
//         int mod = 1e9 + 7;
//         int dp[51][51][101] = {0};

//         // Initialize dp array
//         for (int i = 1; i <= m; ++i) {
//             dp[1][1][i] = 1;
//         }

//         // Fill dp array
//         for (int length = 2; length <= n; ++length) {
//             for (int cost = 1; cost <= k; ++cost) {
//                 for (int maxVal = 1; maxVal <= m; ++maxVal) {
//                     long long sum = 0;

//                     // Carry forward the previous maxVal contributions
//                     sum = (sum + dp[length-1][cost][maxVal] * (long long)maxVal) % mod;

//                     // Consider the new maximum value
//                     for (int prevMax = 1; prevMax < maxVal; ++prevMax) {
//                         sum = (sum + dp[length-1][cost-1][prevMax]) % mod;
//                     }

//                     dp[length][cost][maxVal] = (int)sum;
//                 }
//             }
//         }

//         int result = 0;
//         for (int maxVal = 1; maxVal <= m; ++maxVal) {
//             result = (result + dp[n][k][maxVal]) % mod;
//         }

//         return result;
//     }
// };

// class Solution {
// public:
//     int numOfArrays(int n, int m, int k) {
//         int mod = 1e9 + 7;
//         int dp[51][51][101] = {0};
//         int prefix[51][51][101] = {0};

//         // Initialize dp array
//         for (int i = 1; i <= m; ++i) {
//             dp[1][1][i] = 1;
//         }

//         // Calculate prefix sums for length = 1
//         for (int cost = 1; cost <= k; ++cost) {
//             for (int maxVal = 1; maxVal <= m; ++maxVal) {
//                 prefix[1][cost][maxVal] = (prefix[1][cost][maxVal - 1] + dp[1][cost][maxVal]) % mod;
//             }
//         }

//         // Fill dp array
//         for (int length = 2; length <= n; ++length) {
//             for (int cost = 1; cost <= k; ++cost) {
//                 for (int maxVal = 1; maxVal <= m; ++maxVal) {
//                     long long sum = 0;

//                     // Carry forward the previous maxVal contributions
//                     sum = (dp[length - 1][cost][maxVal] * (long long)maxVal) % mod;

//                     // Add contributions from previous maximum values
//                     if (maxVal > 1) {
//                         sum = (sum + prefix[length - 1][cost - 1][maxVal - 1]) % mod;
//                     }

//                     dp[length][cost][maxVal] = (int)sum;
//                 }
//             }

//             // Update prefix sums for current length
//             for (int cost = 1; cost <= k; ++cost) {
//                 for (int maxVal = 1; maxVal <= m; ++maxVal) {
//                     prefix[length][cost][maxVal] = (prefix[length][cost][maxVal - 1] + dp[length][cost][maxVal]) % mod;
//                 }
//             }
//         }

//         int result = 0;
//         for (int maxVal = 1; maxVal <= m; ++maxVal) {
//             result = (result + dp[n][k][maxVal]) % mod;
//         }

//         return result;
//     }
// };

class Solution {
public:
    int numOfArrays(int n, int m, int k) {
        int mod = 1e9 + 7;

        // Use two layers for current and previous states
        vector<vector<int>> dpPrev(k + 1, vector<int>(m + 1, 0));
        vector<vector<int>> dpCur(k + 1, vector<int>(m + 1, 0));

        // Initialize dp array for length = 1
        for (int i = 1; i <= m; ++i) {
            dpPrev[1][i] = 1;
        }

        // Fill dp array
        for (int length = 2; length <= n; ++length) {
            for (int cost = 1; cost <= k; ++cost) {
                for (int maxVal = 1; maxVal <= m; ++maxVal) {
                    long long sum = 0;

                    // Carry forward the previous maxVal contributions
                    sum = (sum + dpPrev[cost][maxVal] * (long long)maxVal) % mod;

                    // Consider the new maximum value
                    for (int prevMax = 1; prevMax < maxVal; ++prevMax) {
                        sum = (sum + dpPrev[cost-1][prevMax]) % mod;
                    }

                    dpCur[cost][maxVal] = (int)sum;
                }
            }

            // Swap dpCur and dpPrev for the next iteration
            swap(dpCur, dpPrev);
            fill(dpCur.begin(), dpCur.end(), vector<int>(m + 1, 0));
        }

        int result = 0;
        for (int maxVal = 1; maxVal <= m; ++maxVal) {
            result = (result + dpPrev[k][maxVal]) % mod;
        }

        return result;
    }
};



8200