=====Problem Statement=====
You are given a function f(X) = X^2. You are also given K lists. The ith list consists of N_i elements.

You have to pick one element from each list so that the value from the equation below is maximized:
S = (f(X_1) + f(X_2) + ... + f(X_k))%M
X_i denotes the element picked from the ith list. Find the maximized value S_max obtained.
% denotes the modulo operator.
Note that you need to take exactly one element from each list, not necessarily the largest element. You add the squares of the chosen elements and perform the modulo operation. The maximum value that you can obtain, will be the answer to the problem.

=====Input Format=====
The first line contains 2 space separated integers K and M.
The next K lines each contains an integer N_i, denoting the number of elements in the ith list, followed by space separated integers denoting the elements in the list. 

=====Constraints=====
1≤K≤7
1≤M≤1000
1≤N_i≤7
1≤Magnitude of elements in list≤10^9

=====Output Format=====
Output a single integer denoting the value S_max.

Difficulty: introductory

Starter code:

# Enter your code here. Read input from STDIN. Print output to STDOUT

Solutions:
import itertools

k, m = list(map(int,input().split()))

main_ar = []
for i in range(k):
    ar = list(map(int,input().split()))
    main_ar.append(ar[1:])

all_combination = itertools.product(*main_ar)
result = 0
for single_combination in all_combination:   
    result = max(sum([x*x for x in single_combination])%m,result)
print(result)


#!/usr/bin/env python3


from itertools import product

K,M = list(map(int,input().split()))
N = (list(map(int, input().split()))[1:] for _ in range(K))
results = [sum(i**2 for i in x)%M for x in product(*N)]
print((max(results)))