#include <bits/stdc++.h>
using namespace std;
const int N = 1e6, M = N + 10;
const int mod = 998244353, Mod = mod - 1;
inline int add(int a, int b, int p) { return a + b >= p ? a + b - p : a + b; }
inline int sub(int a, int b, int p) { return a >= b ? a - b : a - b + p; }
inline int mul(int a, int b, int p) { return 1ll * a * b % p; }
inline int power(int a, int b, int p) {
  int res = 1;
  for (; b; b >>= 1) {
    if (b & 1) res = mul(res, a, p);
    a = mul(a, a, p);
  }
  return res;
}
int fac[M], ifac[M];
inline void prework(void) {
  fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
  for (int i = 1; i <= N; i++) fac[i] = mul(i, fac[i - 1], mod);
  for (int i = 2; i <= N; i++)
    ifac[i] = mul(sub(mod, mod / i, mod), ifac[mod % i], mod);
  for (int i = 2; i <= N; i++) ifac[i] = mul(ifac[i], ifac[i - 1], mod);
}
inline int C(int n, int m) {
  return mul(fac[n], mul(ifac[m], ifac[n - m], mod), mod);
}
int ans, res, n, sum;
int main() {
  prework();
  cin >> n;
  for (int i = 1; i <= n; i++) {
    res = sub(1, power(power(3, n - i, mod), mod - 2, mod), mod);
    res = power(res, n, mod);
    res = sub(res, 1, mod);
    res = mul(res, power(power(3, 1ll * i * n % Mod, mod), mod - 2, mod), mod);
    res = mul(res, C(n, i), mod);
    if ((i & 1)) res = sub(mod, res, mod);
    ans = add(ans, res, mod);
  }
  res = power(3, 1ll * n * n % Mod + 1, mod);
  ans = mul(res, ans, mod);
  ans = sub(mod, ans, mod);
  for (int i = 1; i <= n; i++) {
    res = power(3, 1ll * n * (n - i) % Mod + i, mod);
    res = mul(res, C(n, i), mod);
    if (i & 1) res = sub(mod, res, mod);
    sum = add(sum, res, mod);
  }
  sum = mul(2, sum, mod);
  sum = sub(mod, sum, mod);
  cout << add(ans, sum, mod) << endl;
  return 0;
}
