WA IN FXSEQ

why this code is giving WA for problem Luv Kush and FX Sequence based on matrix exponentiation.

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define nl "\n"
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define bs binary_search
#define pll pair<ll,ll>
#define ppll pair<ll,pll>
#define sll stack<ll>
#define qll queue<ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vc vector <char>
#define matrix vector<vector<ll>>
#define all(x) x.begin(),x.end()
#define loop(i,s,n) for(ll i=s;i<n;i++)
#define test ll t ; cin>>t; while(t--)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
ll mod = 1000000007;  double pi = 3.1415926535;

ll K = 2;                //* 1-> CHOOSE K
ll n, a, b, c, d, e, f, MOD = 998244353;

// computes A * B
matrix mul(matrix A, matrix B)
{
	matrix C(K + 1, vector<ll>(K + 1));
	loop(i, 1, K + 1)       loop(j, 1, K + 1)     loop(k, 1, K + 1)     C[i][j] = (C[i][j] + (A[i][k] % MOD) * (B[k][j] % MOD)) % MOD;
	return C;
}

// computes A ^ p
matrix pow(matrix A, ll p)
{
	if (p == 1)     return A;
	if (p % 2)      return mul(A, pow(A, p - 1));
	matrix X = pow(A, p / 2);
	return mul(X, X);
}

// returns the N-th term of recurring sequence
ll mat_expo(ll n)
{
	//* 2-> create vector F1
	ll F1[K + 1];
	F1[1] = b;
	F1[2] = a;

	//* 3-> fill matrix T (TRANSFORMATION MATRIX)
	matrix T(K + 1, vector<ll>(K + 1));
	T[1][1] = e, T[1][2] = f;
	T[2][1] = 1, T[2][2] = 0;

	// raise T to the (N-1)th power
	if (n == 0)     return a;
    if (n == 1)	    return b;
	T = pow(T, n - 1);  

	// the answer is the (first row of T) * F1
	ll res = 0;
	loop(i, 1, K + 1)
	{
		res = (res  + ((T[1][i] * F1[i])%MOD)) % MOD;
	}
	
	return res;
}

void solve()
{
	cin >> n >> a >> b >> c >> d >> e >> f;
	a %= MOD;	b %= MOD;	c %= MOD;	d %= MOD;	e %= MOD;	f %= MOD;
	
	ll mfs = mat_expo(n), xs;
	
	if (n % 3 == 0)		xs = c;
	else if (n % 3 == 1)		xs = d;
	else		xs = (c ^ d) % MOD;
	
	ll ans = ((mfs * xs) % MOD) + (((mfs-xs)%MOD+MOD)%MOD) ;
	cout << ans%MOD << nl;
}

int main() {
	fast_io;
#ifndef ONLINE_JUDGE
	freopen("input1.txt", "r", stdin);
	freopen("output1.txt", "w", stdout);
#endif

	test{
		solve();
	}
	return 0;
}

IT WILL BE REALLY HELPFUL IF SOMEONE CAN HELP.
:pray: :pray:
THANK YOU

Can someone tell me what could be the reason that my code is storing only the value obtained during last testcase while i want the sum of all such values :cry:

Send your code if I will find any bug I wll surely help you.