#Haskell, 151 bytes
Haskell, 151 bytes
s n|elem '7'(show n)||mod n 7==0=(0-)|0<1=id a=scanl1(+)$map($1)$scanl(.)id$map s[1..] f m n=mapM_ print[[x+1|x<-[0..m-1],mod(a!!x-1)n==i]|i<-[0..n-1]] *Main> f 30 5 [1,6,8,13,15,19,23,30] [2,7,12,16,18,24] [3,11,17,25] [4,10,21,26,28] [5,9,14,20,22,27,29]