#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, w = 0;
  char c = getchar();
  while (!isdigit(c)) w |= c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
  return w ? -x : x;
}
namespace star {
const int maxn = 1e6 + 10, mod = 998244353;
int n, mul[maxn], inv[maxn];
inline int fpow(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) ans = 1ll * ans * a % mod;
  return ans;
}
inline int C(int n, int m) {
  return 1ll * mul[n] * inv[m] % mod * inv[n - m] % mod;
}
inline int up(int a, int b) { return a < 0 ? a + b : a; }
inline void work() {
  n = read();
  mul[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) mul[i] = 1ll * mul[i - 1] * i % mod;
  inv[n] = fpow(mul[n], mod - 2);
  for (int i = n - 1; i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum =
        (sum + (i & 1 ? mod - 1ll : 1ll) *
                   fpow(3, up((-1ll * i * n) % (mod - 1), mod - 1)) % mod *
                   C(n, i) % mod *
                   (fpow(1 - fpow(3, up(i - n, mod - 1)) + mod, n) - 1 + mod) %
                   mod) %
        mod;
  int ans = 1ll * fpow(3, (1ll * n * n + 1) % (mod - 1)) * sum % mod;
  sum = 0;
  for (int i = 1; i <= n; i++)
    sum = (sum + (i & 1 ? mod - 1ll : 1ll) * C(n, i) % mod *
                     fpow(3, up((i - 1ll * i * n) % (mod - 1), mod - 1))) %
          mod;
  ans = (ans + 2ll * fpow(3, (1ll * n * n) % (mod - 1)) * sum) % mod;
  printf("%d\n", (mod - ans));
}
}  // namespace star
signed main() {
  star::work();
  return 0;
}
