G - Sequence in mod P

AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.