Help me in solving CHMOD problem

My issue

My approach is to make a vector of size n+1 with first element as 1 and store the multiplication of given index in that vector. Since the constraint of n is 10^5 and of a[i] is 100, the multiplication will go till 10^7.then I will just divide the vector’s rth element with (l-1)th element and taking modulo of it I will get my answer. But the submission tells runtime error I don’t know why

My code

#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	// your code goes here
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;++i) cin>>a[i];
    vector<int> ans( n+1 , 1);
    ans[0]=1;
    for(int i=0;i<n;++i) ans[i+1]=ans[i]*a[i];
    int t;
    cin>>t; 
    while(t--){
        int l,r,m;
        cin>>l>>r>>m;
        int final=(ans[r]/ans[l-1]);
        final%=m;
        cout<<final<<"\n";
    }
    return 0;
}

Problem Link: Chef and Segments Practice Coding Problem - CodeChef

@anon44630836
this logic won’t work due to integer overflow problem .
U have to apply some another logic .
here is an appropriate solution

https://www.codechef.com/viewsolution/1028943958

but why is there an integer overflow? The constraint does not even pass the multiplication more than 10^7.

@anon44630836
it overflow when u perform the multiplication.
just think when u multiply 2 100 times it won’t even get stored in long long int.