Returns the total ways to form arrays such that every consecutive element is different and each element except the first and last can take values from 1 to M
static int totalWays ( int N , int M , int X ) {