program modularness
  implicit none
  integer :: k, q, d(0:5000) = 0, n, x, m, a, p, i, j
  read(*,*) k, q
  read(*,*) d(0:k - 1)
  do i = 1, q
    read(*,*) n, x, m
    p = 0
    x = mod(x, m)
    do j = 1, n - 1
      a = mod(x + d(mod(j - 1, k)), m)
      if (x < a) p = p + 1
      x = a
    end do
    write(*,'(i0)') p
  end do
end program modularness