<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

160. Magic Multiplying Machine
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
input: standard input
output: standard output



Well known Las-Vegas casino "Big Jo" has recently introduced the new playing machine, called Magic Multiplying Machine (MMM). MMM has N levers and one big red button. Each lever is marked with some integer number ranging from 1 to M, thus i-th lever is marked with number ai.
A player who wishes to play on MMM inserts a coin into a special slot. After that she chooses some levers and pulls them. After she has made her choice, she presses the big red button. MMM blinks all its lights, rings its bells, plays diRerent tunes and after that declares whether the player has won the game.
The algorithm for determining whether the player has won is the following. If the player has pulled some subset S in {1, 2, ... ,N} of levers, her score is the product of numbers that these levers are marked with, taken modulo M (if the player has pulled no levers, her score is 1):


The player has won the game if her score is maximal possible for the given machine.
Given the description of some MMM determine which levers should be pulled to win the game.

Input
The first line of the input file contains two integer numbers 1 <= N <= 10000 and 2 <= M <= 1 000. The second line contains N integer numbers a1, a2, ..., aN ranging from 1 to M.

Output
In the first line of the output file print one integer number - the score that should be obtained to win the game. In the second line print in ascending order the numbers of levers that should be pulled to obtain this score (levers are numbered starting from one). If there are several solutions, output any one.

Sample test(s)

Input
Sample input #1
4 6
1 2 3 4

Sample input #2
4 4
4 4 4 4

Output
Sample output #1
4
1 4

Sample output #2
1

Note
Author:	Andrew Lopatin, Nikolay Durov
Resource:	ACM ICPC 2002-2003 NEERC, Northern Subregion
Date:	November, 2002

<|response|>
1. Abridged Problem Statement  
You have N levers, the i-th lever labeled with integer ai (1 ≤ ai ≤ M). Pulling a subset S of levers yields a score equal to (∏_{i∈S} ai) mod M (the empty subset scores 1). Find the maximum achievable score and one subset S (report its indices in increasing order) that attains it.

2. Key Observations  
- Since M ≤ 1000, we can keep track of achievable remainders modulo M.  
- For each lever i and each remainder r we’ve reached so far, we have two choices: skip i (remainder stays r) or take i (remainder becomes (r·ai)%M).  
- We only need a boolean table dp[i][r] to mark if remainder r is reachable using the first i levers.  
- To reconstruct which levers were taken, we record a parallel table take[i][r] which is true if we moved into state (i,r) by taking lever i.

3. Full Solution Approach  
a) Initialization  
   - Let dp be an (N+1)×M table of false, except dp[0][1]=true (empty product=1 mod M).  
   - Let take be an (N+1)×M table of false.  

b) DP Transitions  
   For i from 0 to N−1, for each r in [0..M−1] if dp[i][r]==true:  
     - Skip lever i+1: set dp[i+1][r] = true.  
     - Take lever i+1: let r2 = (r * a[i]) % M; set dp[i+1][r2] = true and take[i+1][r2] = true.  

c) Find Answer  
   - Scan r from M−1 down to 0; the first r with dp[N][r]==true is the maximum score. Call it max_r.  

d) Backtrack to Recover Subset  
   - Initialize curr = max_r, an empty list of indices.  
   - For i from N down to 1:  
       * If take[i][curr] is false, we did not take lever i → do nothing.  
       * Otherwise we took lever i:  
           · append i to the answer list,  
           · find a previous remainder prev_r in [0..M−1] such that dp[i−1][prev_r] is true and (prev_r * a[i−1])%M == curr,  
           · set curr = prev_r.  
   - Reverse the list of indices so they are in ascending order.

Time complexity is O(N·M), which with N≤10⁴ and M≤10³ runs in about 10⁷ operations.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    vector<int> a(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        a[i] %= M;  // we only care modulo M
    }

    // dp[i][r]: can we get remainder r using first i levers?
    vector<vector<bool>> dp(N+1, vector<bool>(M, false));
    // take[i][r]: did we take lever i to reach (i, r)?
    vector<vector<bool>> take(N+1, vector<bool>(M, false));

    // Base case: zero levers → product = 1 mod M
    dp[0][1 % M] = true;

    // Fill DP table
    for (int i = 0; i < N; i++) {
        for (int r = 0; r < M; r++) {
            if (!dp[i][r]) continue;
            // Option 1: skip lever i+1
            dp[i+1][r] = true;
            // Option 2: take lever i+1
            int r2 = (r * a[i]) % M;
            dp[i+1][r2] = true;
            take[i+1][r2] = true;
        }
    }

    // Find maximum achievable remainder
    int max_r = 0;
    for (int r = M-1; r >= 0; r--) {
        if (dp[N][r]) {
            max_r = r;
            break;
        }
    }

    // Backtrack to get the chosen levers
    vector<int> result;
    int curr = max_r;
    for (int i = N; i >= 1; i--) {
        if (!take[i][curr]) {
            // Lever i was skipped
            continue;
        }
        // Lever i was taken
        result.push_back(i);
        // Find the previous remainder prev_r that led to curr
        for (int prev_r = 0; prev_r < M; prev_r++) {
            if (dp[i-1][prev_r] && (prev_r * a[i-1]) % M == curr) {
                curr = prev_r;
                break;
            }
        }
    }
    reverse(result.begin(), result.end());

    // Output
    cout << max_r << "\n";
    if (!result.empty()) {
        for (int idx : result) {
            cout << idx << " ";
        }
    }
    cout << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    N = int(next(it))
    M = int(next(it))
    a = [int(next(it)) % M for _ in range(N)]

    # dp[i][r]: can we get remainder r using first i levers?
    dp = [ [False]*M for _ in range(N+1) ]
    # take[i][r]: did we take lever i to reach (i, r)?
    take = [ [False]*M for _ in range(N+1) ]

    # Base: no levers → product = 1 mod M
    dp[0][1 % M] = True

    # Build the DP
    for i in range(N):
        ai = a[i]
        for r in range(M):
            if not dp[i][r]:
                continue
            # Skip lever i+1
            dp[i+1][r] = True
            # Take lever i+1
            r2 = (r * ai) % M
            dp[i+1][r2] = True
            take[i+1][r2] = True

    # Find max achievable remainder
    max_r = 0
    for r in range(M-1, -1, -1):
        if dp[N][r]:
            max_r = r
            break

    # Backtrack to get which levers were taken
    res = []
    curr = max_r
    for i in range(N, 0, -1):
        if not take[i][curr]:
            continue
        # Lever i was used
        res.append(i)
        # Find previous remainder prev_r
        for prev_r in range(M):
            if dp[i-1][prev_r] and (prev_r * a[i-1]) % M == curr:
                curr = prev_r
                break

    res.reverse()  # indices in increasing order

    # Output
    out = []
    out.append(str(max_r))
    if res:
        out.append(" ".join(map(str, res)))
    sys.stdout.write("\n".join(out))

if __name__ == "__main__":
    main()
```