import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as B
main=print.f.map(fst.fromJust.B.readInt).B.words=<<B.getContents
f(_:m:a)=foldr(+)0$map(g.length)$group$sort$scanr(\a s->(a+s)`mod`m)0 a
g i=i*(i-1)`div`2