#include #include using namespace std; namespace my{ using ml=atcoder::modint998244353; auto&operator>>(istream&i,ml&x){int t;i>>t;x=t;return i;} auto&operator<<(ostream&o,const ml&x){return o<<(int)x.val();} #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll ij=n;ij--;) #define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i>T;while(T--)my::solve();}namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<>(istream&i,ulll&x){ull t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<>(istream&i,lll&x){ll t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const lll&x){return o<0?x:-x);} constexpr auto range(bool s,auto...a){arrayr{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;return r;} constexpr char newline=10; constexpr char space=32; void lin(auto&...a){(cin>>...>>a);} templatevoid pp(const auto&...a){ll n=sizeof...(a);((cout<0,c)),...);cout<T floor_sum(lll n,lll m,lll a,lll b){ if(a<0)return-floor_sum(n,m,-a,-b+m-1); if(b<0)return floor_sum(n,m,a,b*(1-m))+(T)b*n; if(a>=m||b>=m)return floor_sum(n,m,a%m,b%m)+(T)(n-1)*n/2*(a/m)+(T)n*(b/m); if(a==0)return 0; return floor_sum((a*n+b)/m,a,m,(a*n+b)%m); } multiple_testcases void solve(){ LL(N,M,L,R); pp(floor_sum(M+1,N-1,-1,R)-floor_sum(M+1,N-1,-1,L+N-2)+M+1); }}