You are given three integers n, m and k. Consider the following algorithm to find the maximum element of an array of positive integers:

You should build the array arr which has the following properties:


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


Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 109 + 7.

&nbsp;
Example 1:

Input: n = 2, m = 3, k = 1
Output: 6
Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]


Example 2:

Input: n = 5, m = 2, k = 3
Output: 0
Explanation: There are no possible arrays that satisify the mentioned conditions.


Example 3:

Input: n = 9, m = 1, k = 1
Output: 1
Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]


&nbsp;
Constraints:


	1 &lt;= n &lt;= 50
	1 &lt;= m &lt;= 100
	0 &lt;= k &lt;= n


class Solution:
    def numOfArrays(self, n: int, m: int, k: int) -> int:
        @cache
        def dp(a,b,c):
            if a==n: return c==k
            return (b*dp(a+1,b,c) if b>=1 else 0) + sum(dp(a+1,i,c+1) for i in range(b+1,m+1))
        return dp(0,0,0)%(10**9+7)
class Solution {
    public int numOfArrays(int n, int m, int k) {
        int M = (int)1e9+7, ans = 0;
        int[][] dp = new int[m+1][k+1]; // maximum value, num of elements seen from left side
        for (int i = 1; i <= m; i++){
            dp[i][1]=1; // base case 
        }
        for (int i = 2; i <= n; i++){
            int[][] next = new int[m+1][k+1];
            for (int j = 1; j <= m; j++){ // for the current max value
                for (int p = 1; p <= m; p++){ // previous max value
                    for (int w = 1; w <= k; w++){ // for all possible k
                        if (j>p){ // if current max is larger, update next[j][w] from dp[p][w-1]
                            next[j][w]+=dp[p][w-1];
                            next[j][w]%=M;
                        }else{ // otherwise, update next[p][w] from dp[p][w]
                            next[p][w]+=dp[p][w];
                            next[p][w]%=M;
                        }
                    }
                }
            }
            dp=next;
        }
        for (int i = 1; i <= m; i++){ // loop through max that has k and sum them up.
            ans += dp[i][k];
            ans %= M;
        }
        return ans;
    }
}
class Solution {
public:
    int numOfArrays(int n, int m, int k) {
        if(m<k)return 0;
        int dp[2][m+1][k+1],mod=1e9+7;
        memset(dp,0,sizeof(dp));
        for(int j=1;j<=m;++j)
            dp[0][j][1]=j;
        for(int i=1;i<n;++i)
            for(int j=1;j<=m;++j)
                for(int l=1;l<=min(i+1,min(j,k));++l)
                    dp[i&1][j][l]=(dp[i&1][j-1][l]+(long)(dp[(i-1)&1][j][l]-dp[(i-1)&1][j-1][l])*j+dp[(i-1)&1][j-1][l-1])%mod;
        return (dp[(n-1)&1][m][k]+mod)%mod;
    }
};
var numOfArrays = function(n, m, k){ 
    let mod=1e9+7,
        // dp[i][c][j] the number of arrays of length i that cost c and their max element is j  
        dp=[...Array(n+1)].map(d=>[...Array(k+1)].map(d=>[...Array(m+1)].map(d=>0))),
        // prefix[i][k][j] holds the prefix sum of dp[i][k][:j] 
        prefix=[...Array(n+1)].map(d=>[...Array(k+1)].map(d=>[...Array(m+1)].map(d=>0)))
    //basecases
    dp[0][0][0] = 1
    prefix[0][0].fill(1)
    for(var i = 1; i <= n; i++) //length of array
        for(var x = 1; x <= k; x++) //curcost
            for(var j = 1; j <= m; j++) //curmax
                // the previous max can be anything <j with a cost of x-1, to which we append j 
                dp[i][x][j] += prefix[i-1][x-1][j-1],
                // we can also append any number <=j to an array of length i-1 that already costs x,
                //therefore not increasing the cost
                dp[i][x][j] += dp[i - 1][x][j] *j ,
                dp[i][x][j] %= mod,
                prefix[i][x][j]=(prefix[i][x][j-1]+dp[i][x][j])%mod
    return dp[n][k].reduce((a,c)=>(a+c)%mod)
};
Build Array Where You Can Find The Maximum Exactly K Comparisons