Problem: Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
Given an array arr[] of N distinct integers and a positive integer M, the task is to find the product of all the elements at the indexes which are the factors of M for all the possible sorted subsequences of length M from the given array arr[].
Note: The product may be very large, take modulo to 109 + 7.

Sample Input: arr[] = {7, 8, 9}, M = 2
Sample Output: 254016

Solution (java):
// Java program to find the product of
// all the combinations of M elements
// from an array whose index in the
// sorted order divides M completely
import java.util.*;

class InfobayAI{

static int m = 4;

// Iterative Function to calculate
// (x^y)%p in O(log y)
static long power(long x, long y, long p)
{
    long res = 1;
    x = x % p;

    while (y > 0)
    {

        // If y is odd, multiply 
        // x with result
        if (y % 2 == 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}

// Iterative Function to calculate
// (nCr)%p and save in f[n][r]
// C(n, r)%p = [ C(n-1, r-1)%p + C(n-1, r)%p ] % p
// and C(n, 0) = C(n, n) = 1
static void nCr(int n, long p, int f[][])
{
    for(int i = 0; i <= n; i++) 
    {
       for(int j = 0; j <= m; j++)
       {
          
          // If j>i then C(i, j) = 0
          if (j > i)
              f[i][j] = 0;
          
          // If i is equal to j 
          // then C(i, j) = 1
          else if (j == 0 || j == i)
              f[i][j] = 1;
          
          // C(i, j) = ( C(i-1, j) + C(i-1, j-1))%p
          else
              f[i][j] = (f[i - 1][j] + 
                         f[i - 1][j - 1]) % (int)p;
       }
    }
}

static void operations(int arr[], int n, int f[][])
{
    long p = 1000000007;
    nCr(n, p - 1, f);

    Arrays.sort(arr);

    // Initialize the answer
    long ans = 1;

    for(int i = 0; i < n; i++)
    {
       
       // For every element arr[i],
       // x is count of occurrence
       // of arr[i] in different set
       // such that index of arr[i]
       // in those sets divides m 
       // completely.
       long x = 0;
       
       for(int j = 1; j <= m; j++)
       {
          
          // Finding the count of arr[i]
          // by placing it at the index
          // which divides m completely
          if (m % j == 0)
              
              // Using fermat's little theorem
              x = (x + (f[n - i - 1][m - j] * 
                               f[i][j - 1]) % 
                                   (p - 1)) % 
                                   (p - 1);
       }
       
       // Multiplying with the count
       ans = ((ans * power(arr[i], x, p)) % p);
    }
    System.out.print(ans + "
");
}

// Driver code
public static void main(String[] args)
{
    int arr[] = { 4, 5, 7, 9, 3 };
    int n = arr.length;
    int [][]f = new int[n + 1][m + 1];

    operations(arr, n, f);
}
}